Python __dict__ Properties in a detailed

Source: Internet
Author: User

We all know what Python is all about, so how does Python manage the object?

1. Ubiquitous __dict__

First look at the __dict__ property of the class and the __dict__ property of the class object

#-*-coding:utf-8-*-classA (object):"""Class A. """a=0 B= 1def __init__(self): SELF.A= 2self.b= 3defTest (self):Print 'a normal func.'@staticmethoddefstatic_test (self):Print 'a static func.'@classmethoddefclass_test (self):Print 'a calss func.'obj=A ()PrintA.__dict__PrintObj.__dict__

The results of the operation are as follows:

{'a': 0,'__module__':'__main__','b': 1,'class_test': <classmethod object at 0x00000000021882e8>'__dict__': <attribute'__dict__'Of'A'Objects>,'__init__': <function__init__At 0x00000000023a5ba8>'Test': <function test at 0x00000000023a5c18>'__weakref__': <attribute'__weakref__'Of'A'Objects>,'__doc__':'\ n Class a.\n','static_test': <staticmethod object at 0x00000000021881c8>}{'a': 2,'b': 3}

Thus, the static functions, class functions, ordinary functions, global variables, and some built-in properties of a class are all placed in the class __dict__ .

  Some of the self.xxx things are stored in the __dict__ of the object .

2. What doesn't have a __dict__ attribute in Python

Although everything is the object, but the object is also different, just like not everyone's girlfriend is a person, some of the built-in data types are not __dict__ properties, as follows:

num = 3== {}print num.__dict__print LL.__dict__  Print dd.__dict__

When we run this code, the interpreter will tell us

Traceback (most recent): File"f:\python\test.py", Line 54,inch<module>PrintNum.__dict__Attributeerror:'int'object has no attribute'__dict__'Traceback (most recent): File"f:\python\test.py", line 55,inch<module>Printll.__dict__Attributeerror:'List'object has no attribute'__dict__'Traceback (most recent): File"f:\python\test.py", line 56,inch<module>PrintDd.__dict__Attributeerror:'Dict'object has no attribute'__dict__'

These commonly used data types, such as int, list, dict, and so on, are not __dict__, which is predictable, even if given their dict properties, they are used only for data containers.

3. The __dict__ attribute when the inheritance occurs

Subclasses have their own __dict__, and the parent class has its own __dict__, the global variables and functions of the subclasses are placed in the dict of the subclass, and the parent class is placed in the parent class Dict.

#-*-coding:utf-8-*-classParent (object): a=0 B= 1def __init__(self): SELF.A= 2self.b= 3defp_test (self):PassclassChild (Parent): a= 4b= 5def __init__(self): super (Child, self).__init__() # SELF.A= 6# self.b= 7defc_test (self):Pass    defp_test (self):PassP=Parent () C=Child ()PrintParent.__dict__PrintChild.__dict__PrintP.__dict__PrintC.__dict__

Run the above code, and the result is entered below:

{' A ': 0,'__module__':'__main__',' B ': 1,'__dict__': <attribute'__dict__'Of'Parent'Objects>,' p_test ': <function p_test at 0x0000000002325ba8>,'__weakref__': <attribute'__weakref__'Of'Parent'Objects>,'__doc__': None,'__init__': <function__init__At 0x0000000002325b38>}{' A ': 4,'c_test': <function c_test at 0x0000000002325c88>'__module__':'__main__',' B ': 5,' p_test ': <function p_test at 0x0000000002325cf8>,'__doc__': None,'__init__': <function__init__At 0x0000000002325c18>}{' A ': 2, ' B ': 3} {' A ': 2, ' B ': 3}

1) in the output of the above paragraph, the class variables and functions are marked in red, and the result shows that the class variables and function names of each class are placed in their __dict__.

2) Look again at the __dict__ of the strength variable, identified by the blue font, the __dict__ of the parent class and the subclass object are common

Summarize:

1) Built-in data type without __dict__ property

2) Each class has its own __dict__ attribute, and even if the inheritance relationship exists, the __dict__ of the parent class does not affect the __dict__ of the subclass.

3) objects also have their own __dict__ properties, storing self.xxx information, and parent-child class objects common __dict__

Python __dict__ Properties in a detailed

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.