Python a note of the place

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.