Classes and instances in python

  python classes and c ++ classes are the same, except that c ++ class, if containing member variables and member variable is changed, the object (instance) will be the class or subclasses of the object inherited, but if the python has its own properties that instance properties operated before the instance of the class, regardless of class property is changed after that, we have nothing to do with it, and the value is not inherited. For example, the class is constructed by a number of methods. The following example of a paste, from example: http://onlypython.group.iteye.com/group/wiki/1357-to-talk-about-the-types-of-properties-in-python-and-examples -of-the-types-of- attributes-the-difference

# Coding: UTF. 8- 

class the AAA (): 
    AAA = 10 # situation. 1 
OBJ1 = the AAA () 
obj2 = the AAA () 
 Print obj1.aaa, obj2.aaa, AAA.aaa # case 2 
obj1.aaa + 2 = Print OBJ1 .aaa, obj2.aaa, AAA.aaa # situation. 3 
AAA.aaa + =. 3 Print obj1.aaa, obj2.aaa, AAA.aaa


    




View Code

  A situation, it is clear, by instances of the class generated to access the class attribute, because there is no change, so they value the three are the same.

  Case 2, obj1.aaa + = 2, aaa obj1 is the class from which it is available to the AAA, but it is only the value inherited from the other side of the + operation performed, and did not affect the properties of class aaa in AAA , while obj2 the aaa has not changed, so it does not change.

  Case 3, AAA.aaa its properties aaa performed an operation, like the above when it comes to the "If you have their own attributes that instance properties operated prior instance of the class, after that regardless of the type of property is changed, nothing to do with it, and the value is not inherited. " Here is what it means.

  Properties of objects in python, can be viewed by __dict__. So you can join in a few lines of code above, print instance / class .__ dict__ to the list of attributes and instance class, python in all are called objects, of course, whether it is an instance of the class or object we are called, this concept is very different with c ++.

  If it is not particularly clear for the concept of classes and instances, go here: http://ipseek.blog.51cto.com/1041109/802243 

Reproduced in: https: //www.cnblogs.com/RainingDays/p/3270787.html

Guess you like

Origin blog.csdn.net/weixin_34167043/article/details/94538760