This is correct behaviour. I would suggest that you post this to python-list for a full discussion of what's going on here, but basically the default value for argument bar of __init__ is created at class creation time, and then reused for every instance. This is a common mistake made by newcomers, using mutable default arguments for functions (or methods). A google search for "python mutable default argument" should find you some useful explanations of what's going on. Cheers, Paul On 21 April 2017 at 10:47, Justus Schwabedal <jschwabedal at gmail.com> wrote: > Hi everyone, > > I possibly found a bug in class __init__ and would like to fix it. So I'm > looking for a mentor to help me. > > `class Foo: > def __init__(self, bar=[]): > self.list = bar > > spam_1 = Foo() > spam_2 = Foo() > > spam_1.list.append(42) > print(spam_2.list)` > > At least I think it's a bug. Maybe it's a feature.. > > Best Regards, Jus > > > > > > -- > Justus Schwabedal > > Handy (D): +49 177 939 5281 > email: jschwabedal at googlemail.com > skype: justus1802 > > Görlitzer Str. 22 > 01099 Dresden, Sachsen > Germany > > Steinkreuzstr. 23 > 53757 Sankt Augustin, NRW > Germany > > _______________________________________________ > Python-Dev mailing list > Python-Dev at python.org > https://mail.python.org/mailman/listinfo/python-dev > Unsubscribe: > https://mail.python.org/mailman/options/python-dev/p.f.moore%40gmail.com >
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4