On 21 April 2017 at 12:09, Justus Schwabedal <jschwabe...@gmail.com> wrote:
> I possibly found a bug in class initialization and would like to fix it.
>
> Here comes the bug-producing example:
>
> `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..
>

It is not a bug.

It is the way in which Python handles mutable keyword arguments.
If you want to use something in this way you should go with

def __init__(self, bar=None):
    if bar is None:
        bar = []
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to