Python is all objects (object), and each object may have multiple properties (attribute). Its properties may come from a class definition or class inheritance, which is called a class property , or it may come from a property of an instance object, which is called an instance property .
Instance properties may be different for different instances, and the class properties of the different instances are the same. Therefore, the property that needs the user's passing in as an instance attribute is generally used, and the same attribute is used as the class attribute.
Property
Define properties: class. property = Value or instance. property = Value
Call Properties: Class (). Property ( ) or instance. Property () or instance. Property
1>>>classPerson :2 defThink (self):3 Print "To being or not to being is a question"4 5>>>classWomen (person):6 defGender (self):7 Print "She is a girl or a lady"8 9 Ten>>> Ryana = Women ()#instantiate a subclass of women object Ryana One>>>Ryana.think () # by instance. Property () Call ATo ISor notTo IS isa question ->>>women (). Gengder () #通过类 (). property () Invocation, Class () equivalent to class instance -She is a girlora Lady the>>> Ryana.age = 18#Defining Instance Properties ->>> Person.speak ='Hello' #class, define class properties ->>> Ryana.speak#Note there is no () because the string property of the class is called, not the class method - 'Hello ' +>>> person.__dict__ -{'__module__':'__main__','speak':'Hello','Think': <function think at 0x03dedef0>'__doc__': None} +>>> women.__dict__ A{'Gengder': <function Gengder at 0x03dedf30>'__module__':'__main__','__doc__': None} at>>> Ryana.__dict__ -{' Age': 18} ->>> Echo = person ()#instantiate a parent class person object echo ->>>Echo.think () -To ISor notTo IS isa question ->>>Echo.speak in 'Hello' ->>> Echo.gender#class is inherited from bottom to top to + Traceback (most recent): -File"<pyshell#20>", Line 1,inch<module> the Echo.gengder *Attributeerror:person instance has no attribute'Gender' $>>> Echo.__dict__Panax Notoginseng{}
Querying the properties of a class or object in addition to accessing the class's Dictionary property __dict__, you can also use the built-in function, dir (), to view all the properties of a class or object.
>>>dir (person) ['__doc__','__module__','speak','Think']>>>dir (women) ['__doc__','__module__','Gengder','speak','Think']
From the above attribute, except for the class custom properties, there are __doc__,__module__, such properties are called special properties, the common special properties are:
1>>> women.a tuple of all the parent classes of the __bases__ # class 2(<class __main__. Person at 0x03834ed8>,)3>>> women. Document string for __doc__ # class 4>>> women.__name__ # class name (string) 5 'Women'6>>> women.__module__ # The module where the class definition resides 7 '__main__'8>>> Ryana.__class__ # classes corresponding to instances 9<class __main__. Women at 0x03c7a5e0>
Class properties conflict with instance properties
1>>>classF:2A = 103 4 #instantiating objects Obj1 and obj2 inherit property A from class F5>>> obj1 =f ()6>>> Obj2 =f ()7>>>Printobj1.a, obj2.a, F.A810 10 109 Ten #instantiating an object Obj1 custom instance Properties obj1.a = One>>> obj1.a = obj1.a + 2# equivalent to obj1.a + = 2 A>>>Printobj1.a, obj2.a, F.A -12 10 10 - the #changing the properties of Class F, because the object Obj1 instance property A and Class F class Property a naming conflict, so object Obj1 will mask class properties ->>> F.A = F.A + 3 ->>>Printobj1.a, obj2.a, F.A -12 13 13
The properties in Python are looked up from bottom to top, and when the instance properties are removed and the same name is used, the Class property is accessed.
Fundamentals Review: Properties