Explanation of class attributes and data properties in Python

Source: Internet
Author: User
Tags class definition naming convention

The class in Python is called Class object, and the instance of the class is called instance object.
Class Objects
Class has two operations, 1. Class properties attribute references 2. Instantiation instantiation
A class property is equivalent to a variable that belongs to a class (that is, static public variables of a class in some languages) by using the class name. Instantiation of a class property name is a method for creating an instance of a class, using the following method: Class name ()
After using the instantiation method, an empty class instance is created, and a special method is initialized in the definition of the generic Python class, which is __init__ (), and when the instantiation method of the class is called, The __init__ () method is immediately called by an instance of the class. That is, __init__ () is not a constructor, but an ordinary method. Instance of class Instance Objects
The instance of the class has only one operation, which is 1. The property calls attribute references.
Property invocation refers to 1. Data Properties 2. Methods
Data attributes do not need to be pre-defined! When the data attribute is first used, it is created and assigned (they spring into existence when they be first assigned to) see the following example

Class Test:passt=test () t.name= ' notus ' Print T.name


We do not define the data property of name in class test, but it can be used directly in code, which is the data property. Now, aside from the generalized interpretation of attribute attribute, the term often used in real programming, there are two properties in Python's class: Class attribute, data attribute. (Most programming languages have two properties). Class properties belong to a class, and data properties belong to instances of the class. We assume that there is a class test, and the general use of these two properties is

Test.modet=test () T.name

So when are these two properties supposed to be defined?
According to the above discussion, the data attribute does not need to be predefined, and when the data attribute is first used, it is created and assigned. In fact, the class attribute is the same.
So, we have the following example

Class Test:passt=test () t.name= ' notus ' Print t.nametest.mode= ' auto ' print Test.mode

As you can see, the Data property name and the class attribute mode are not defined in the class, you just have to use them when you need them. How to assign a value to a property in advance
You can use attributes in the definition of a class, first look at this example

Class Test:def Ask (theinstance): Theinstance.name= ' notus ' test.mode= ' auto ' # #print test.modet=test () # #print T.namet.ask () Print Test.modeprint t.name

Class test has method ask. Note that the two lines commented out in the program, before the Ask () method is used, any sentence that runs the annotated sentence will be faulted, prompting "class Test has no attribute ...". But after running ask (), we ask () The two properties are initialized in the method and run through.
Note that the Ask () received parameter theinstance, which is passed to the program in the class test instance T. The general naming convention suggests that this parameter be named self. This parameter is automatically passed in by Python, so it doesn't need to be passed in the program.
If you want to use these properties immediately after the class is instantiated, you should place the initial properties in the __init__ () method, as previously stated, the __init__ () method is automatically called immediately after the instantiation of the class. So our example program can be changed into this.

Class Test:def __init__ (self): self.name= ' notus ' test.mode= ' auto ' # #print test.modet=test () # #print T.nameprint Test.modeprint T.name


So you can have such a class definition

Class Test:def __init__ (self): self.name= ' notus ' test.mode= ' auto ' def ask: self.date= ' # #print test.modet= Test () # #print t.nameprint test.modeprint t.name# #print t.datet.ask () print t.date


Data attribute date is only available if the Ask () method is called. Of course, this property can also be used

# #print Test.modet=test () # #print t.nameprint test.modeprint t.namet.date= ' Print t.date


The Data property of date is created in the program. As you can imagine, when you call the Ask () method, the data attribute date already exists, but the value is changed. You can initialize a property without a method
See the example program below

Class Test:action= ' win the game '  #类属性print test.actiont=test () print t.actiontest.action= ' At least 1 point ' print Test.actionprint t.actiont.action= ' dont lose ' print test.actionprint t.action


The results of the operation are as follows

Win the game
Win the game
At least 1 point
At least 1 point
At least 1 point
Dont lose
The phenomenon can be summed up as: "Change the class properties, data properties, change data properties, class properties unchanged."

Class AAA ():  AAA = ten  #类属性情形1   obj1 = AAA ()  obj2 = AAA ()   print obj1.aaa, obj2.aaa, aaa.aaa   case 2
   OBJ1.AAA + = 2  print obj1.aaa, obj2.aaa, aaa.aaa   case 3  aaa.aaa + 3  print obj1.aaa, OBJ2.AAA, AAA.AAA


Reference
For Scenario 1, I believe most people will say the result correctly, that is:
10 10 10
For this result, there is no suspense, the same answer is given by two AAA instances, and the AAA attribute value referenced by the AAA class name. In case 2, what should be the result, I believe most people will still say the correct result:
12 10 10
In the above result, once executed obj1.aaa + = 2, also means that obj1 this instance has an instance of the attribute value, his property name is AAA, that is not obj1 AAA is a new attribute, actually can say is right, but also wrong, actually obj1.aaa + = 2 The execution of this code is not as simple as we think, first he will go to the Obj1 Class AAA attribute list to find a property called AAA, if any, he will return the value as the initial value of AAA in Obj1, that is, After this, the OBJ1.AAA attribute value is basically not related to AAA.AAA. In case 3, what is the answer:
12 13 13
This, in fact, is very simple, aaa.aaa to the AAA class attributes have been set once, obj1.aaa after a + = operation, In fact, aaa.aaa out of the relationship, and OBJ2.AAA does not undergo any property manipulation, so it will only get AAA from the Class AAA it belongs to, and return.

Reference
"Python core Programming"
If you try to set or update a class property in an instance, an instance property of C.version is created, which blocks the class property
C.versioin access, because the first access is c.version, which effectively "obscures" the class property c.version until C.version is cleared out.

Explanation of class attributes and data properties in Python

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.