Steven D'Aprano wrote:
> Python function default values use *early binding*: the default parameter
> is evaluated, ONCE, when the function is defined, and that value is used
> each time it is needed.
Thanks, "early binding" was the clue I was missing.
robert
--
https://mail.python.org/mailman/
On Thu, 03 May 2018 19:47:37 +, Robert Latest via Python-list wrote:
> Hello,
>
> I don't understand the behavior of the code below. Why does the dict
> property "a" of both objects contain the same keys? This is only if
> "a=dict" is in the initializer. If I put self.a = dict() into the init
This is a well known feature of Python. It's a very common "gotcha" to
new Python programmers.
Google "Mutable default parameters in Python" for long list of
explanations and fixes.
In short, don't use a mutable object as a default parameter.
Gary Herron
On 05/03/2018 12:47 PM, python-
Hello,
I don't understand the behavior of the code below. Why does the dict property
"a" of both objects contain the same keys? This is only if "a=dict" is in
the initializer. If I put self.a = dict() into the init function, I get two
separate dicts
class Foo(object):
def __init__(self, x,