Python class properties, instance properties, variable data structures as a class attribute need to be noted where

Source: Internet
Author: User

There is a big stupid pig, according to the Java write observer mode, Java is written directly under the class name of an instance attribute (not static), he directly translated into Python, is directly written under the class name, this is the big hole.

Java is declared under the class name with the static Class property, without static as the instance property, Python if the property directly written under the class name, are all class properties, the instance properties are usually written in Init, with the self word of the variable.

#coding: Utf-8class A ():    x_list = []A1 = A () a1.x_list.append (1) a2 = A () print (' first result---', A2.x_list) class B ():    x = 0b1 = B () b1.x =1b2 = B () print (' second result ', b2.x) class C ():    x = 0C1 = C () c.x =1c2 = C () print (' second result ', c2.x)

The result of the operation is this.

Compared to a B C three classes can be found that the class property itself, the instance property is equal to the Class property by default.

For example, Class B, instance B1 x is equal to 0, when assigned to 1, B1 x equals 1, at this point the Class B attribute x is still equal to the 0,B2 instance's attribute x is equal to 0.

Class C, change the x attribute of Class C directly, then the X of C becomes 1, and if a new C2 is instantiated, the default is equal to 1.

B and C This operation is in many cases, bad street, it is necessary to see the common error-prone cases.

The most important is a class, a class of x_list is a list, the list is a mutable data structure, with a variable data structure as a class property, it is very careful, even with the instance A1 Operation X_list, will still affect the properties of a x_list and A2 properties x_list. When a mutable element is a class attribute, the operations a.x_list and a.x_list are equivalent and are mutually affected.

Examples of immutable data structures:

a_str = ' Hello '

B_str = A_str

B_str = a_str + ' world '

The value of the final A is Hello, and the value of B is Hello World.

Examples of mutable data structures:

a_list = [' Hello ']

B_list = A_list

B_list + = [' World ']

The final values of a_list and b_list are [' Hello ', ' world ']

Back to the question, in observer mode, if you add subscribers to the class attribute X_lsit list, all Publishers share all subscribers.

For example, Xiao Ming, small army subscribed to Cang teacher, small red small yellow to subscribe to the wave teacher, but Cang teacher update the film, due to share the subscribers, led to small red and small yellow also received new film notice, this is obviously a mistake.

So Cang teacher and wave teacher X_list must be independent, need to write in __init__ inside, written self.x_list, so Cang teacher and wave Teacher's x_list are all independent, add subscribers to Cang Teacher's list, wave Teacher's list will not follow change.

Python class properties, instance properties, variable data structures as a class attribute need to be noted where

Related Article

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.