https://www.zhihu.com/question/25874136
Class Test: l=[] def init(self): self. L=[' 1 ',' 2 ',' 7 ']a1=Test()A1. Init()
If I define a class like this, after executing the Init method, the value of A1.L is [' 1 ', ' 2 ', and ' 7 '],test.l the value is [].
But if you change it like this:
ClassTest:L=[] def init (): self. L. Append (1) self. L. Append (2) self. L. Append (7) a1= Test () a1. Init ()
Then the value of TEST.L will become [1,2,7].
the difference between the two is that SELF.L = Blablabla This is an
assignment statement, Self.l.append (BLABLABLA) This is not an assignment statement.
when Python intepreter sees that you are using the variable name "SELF.L", it does not know whether you want to modify a variable that is visible in scope or create a new variable in this scope. The logic is simple: as long as you use the assignment, you want to create a new variable by default.
Python a note of the place