Message384765
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! |
|
Date |
User |
Action |
Args |
2021-01-10 14:43:44 | haoyang9804 | set | recipients:
+ haoyang9804 |
2021-01-10 14:43:44 | haoyang9804 | set | messageid: <1610289824.57.0.574510344597.issue42883@roundup.psfhosted.org> |
2021-01-10 14:43:44 | haoyang9804 | link | issue42883 messages |
2021-01-10 14:43:44 | haoyang9804 | create | |
|