<div dir="ltr">And it is not related to __init__ method.<div>You have the same behaviour with any other function or method.</div><div><br></div><div><div>>>> def append_to_list(item, l=[]):</div><div>...     l.append(item)</div><div>...     return l</div><div>... </div><div>>>> append_to_list(1)</div><div>[1]</div><div>>>> append_to_list(2)</div><div>[1, 2]</div></div></div><div class="gmail_extra"><br><div class="gmail_quote">2017-04-21 17:18 GMT+02:00 Manolis Mavrofidis <span dir="ltr"><<a href="mailto:mmavrofides@gmail.com" target="_blank">mmavrofides@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">In a nutshell.<br>
You create two instances and you assign the same list to both of them<br>
which you instantiate when you run your code.<br>
>>> id(spam_1.list)<br>
4530230984 # <- Here<br>
>>> id(spam_2.list)<br>
4530230984 # <- Here<br>
>>> id(spam_1)<br>
4530231632 # Nice unique instance<br>
>>> id(spam_2)<br>
4530231200 # Nice unique instance as well<br>
<br>
Try<br>
>>> class Foo:<br>
...     def __init__(self, list=None):<br>
...             self.list = list<br>
...<br>
>>> spam_1 = Foo()<br>
>>> spam_2 = Foo([]) <- Cheating.<br>
>>> spam_1<br>
<__main__.Foo instance at 0x10e05d9e0><br>
>>> spam_2<br>
<__main__.Foo instance at 0x10e05d950><br>
>>> spam_2.list.append(42)<br>
>>> print(spam_1.list)<br>
None<br>
>>> print(spam_2.list)<br>
[42]<br>
>>> id(spam_1.list)<br>
4527705752<br>
>>> id(spam_2.list)<br>
4530231488<br>
<br>
Or something along those lines :)<br>
<div><div class="h5"><br>
On 21 April 2017 at 16:03, Guyzmo via Python-Dev <<a href="mailto:python-dev@python.org">python-dev@python.org</a>> wrote:<br>
> On Fri, Apr 21, 2017 at 11:47:24AM +0200, Justus Schwabedal wrote:<br>
>> At least I think it's a bug.  Maybe it's a feature..<br>
><br>
> it's indeed a feature.<br>
><br>
>> I possibly found a bug in class __init__ and would like to fix it<br>
><br>
> technically, it's a method. More precisely, it's the constructor method.<br>
><br>
>> So I'm looking for a mentor to help me.<br>
>><br>
>> class Foo:<br>
>>     def __init__(self, bar=[]):<br>
>>         self.list = bar<br>
>><br>
>> spam_1 = Foo()<br>
>> spam_2 = Foo()<br>
>><br>
>> spam_1.list.append(42)<br>
>> print(spam_2.list)`<br>
><br>
> the argument `bar` of your method is instanciated at the time you're<br>
> declaring the method. It's happening once for the the lifetime of the<br>
> execution of your code.<br>
><br>
> By allocating the `bar` reference into the `self.list` member, you're<br>
> assigning the same *instance* of that list into the `self.list` member.<br>
><br>
> So everytime you create a new Foo instance, you're actually assigning<br>
> the same `[]` instance into `self.list` which is why, when you mutate<br>
> the list, it's happening in all the instances of Foo as well.<br>
><br>
> I hope it makes sense to you !<br>
><br>
> --<br>
> Guyzmo<br>
> ______________________________<wbr>_________________<br>
> Python-Dev mailing list<br>
> <a href="mailto:Python-Dev@python.org">Python-Dev@python.org</a><br>
> <a href="https://mail.python.org/mailman/listinfo/python-dev" rel="noreferrer" target="_blank">https://mail.python.org/<wbr>mailman/listinfo/python-dev</a><br>
</div></div>> Unsubscribe: <a href="https://mail.python.org/mailman/options/python-dev/mmavrofides%40gmail.com" rel="noreferrer" target="_blank">https://mail.python.org/<wbr>mailman/options/python-dev/<wbr>mmavrofides%40gmail.com</a><br>
<span class="HOEnZb"><font color="#888888"><br>
<br>
<br>
--<br>
"Only those who will risk going too far<br>
can possibly find out how far one can go.<br>
 "T.S. Eliot<br>
<a href="http://0x109.tuxfamily.org" rel="noreferrer" target="_blank">http://0x109.tuxfamily.org</a><br>
</font></span><div class="HOEnZb"><div class="h5">______________________________<wbr>_________________<br>
Python-Dev mailing list<br>
<a href="mailto:Python-Dev@python.org">Python-Dev@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-dev" rel="noreferrer" target="_blank">https://mail.python.org/<wbr>mailman/listinfo/python-dev</a><br>
Unsubscribe: <a href="https://mail.python.org/mailman/options/python-dev/antoine.rozo%40gmail.com" rel="noreferrer" target="_blank">https://mail.python.org/<wbr>mailman/options/python-dev/<wbr>antoine.rozo%40gmail.com</a><br>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr">Antoine Rozo</div></div>
</div>