Re: Weird side effect of default parameter

2018-05-07 Thread Robert Latest via Python-list
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 --

Re: Weird side effect of default parameter

2018-05-04 Thread Steven D'Aprano
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

Re: Weird side effect of default parameter

2018-05-03 Thread Gary Herron
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,

Weird side effect of default parameter

2018-05-03 Thread Robert Latest via Python-list
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,