27. Properties of the __DICT__ System 1) object may come from:
- The definition of its class, called the Class attribute
- Inherit the definition of the parent class
- The object instance definition (assigned value when initializing the object), called the object property
2) The properties of the object are stored in the object's
__dict__In the properties:
- __dict__ is a dictionary, the key is the property name, and the value is the property itself.
Example:
Class Bird (object): feather = True # parent Class Class chicken (bird): fly = False def __init__ (self, age ): Self.age = Age # subclass Summer = Chicken (2) # Subclass of Object Print (bird.__dict__) # Parent Class Property
{' __dict__ ': <attribute ' __dict__ ' of ' bird ' objects>, ' __module__ ': ' __main__ ', ' __weakref__ ': < Attribute ' __weakref__ ' of ' bird ' objects>, 'feather': True, ' __doc__ ': None}
Print (chicken.__dict__) # sub-class properties
{'fly': False, ' __module__ ': ' __main__ ', ' __doc__ ': None, '__init__': <function __init__ at 0x2b91db476d70>}
Print (summer.__dict__) #对象属性
{'age ': 2}
3) The attributes are defined hierarchically:
For example, the above is divided into Object/bird/chicken/summer four layers.
When we need to invoke a property, Python walks up one layer at a time until the nearest property is found.
__class__ and __base__
The __class__ property can help the object to query its class;
The __base__ property can query its parent class through these class
4) Modify/reference the properties of the object
The following two ways are equivalent to each other:
Summer.__dict__[' age '] = 3 print (summer.__dict__[' age ')
Summer.age = 3 print (summer.age)
28.
Python Learning note 7 (Class/Object properties)