This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author haoyang9804
Recipients haoyang9804
Date 2021-01-10.14:43:44
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1610289824.57.0.574510344597.issue42883@roundup.psfhosted.org>
In-reply-to
Content
Here is the bug-triggered code snippet in the file uploaded

class A:
    def __init__(self, b=[]):
        print('b = ', b)
        self.a = b 

for i in range(3):
    a = A()
    a.a.append(1)

    print(a.a)

It seems that when I pass a list "b" to __init__ function with default value empty list. Every time I create a new instance of this class and append one new variable to "self.a", the default value of "b" changed at the next time I create another instance of class A.

The outcome of this code snippet is 
a =  []
[1]
a =  [1]
[1, 1]
a =  [1, 1]
[1, 1, 1]

I am new to python. Is it a legal behavior in python? If yes, what is the principle beneath it? Thanks in advance!
History
Date User Action Args
2021-01-10 14:43:44haoyang9804setrecipients: + haoyang9804
2021-01-10 14:43:44haoyang9804setmessageid: <1610289824.57.0.574510344597.issue42883@roundup.psfhosted.org>
2021-01-10 14:43:44haoyang9804linkissue42883 messages
2021-01-10 14:43:44haoyang9804create