Python Learning note 7 (Class/Object properties)

Source: Internet
Author: User

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)

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.