Based on the differences between Python _ dict _ and dir (), python _ dict _

Source: Internet
Author: User
Tags print object

Based on the differences between Python _ dict _ and dir (), python _ dict _

In Python, everything is an object. Each object has multiple attributes. Python has a unified management solution for attributes.

Differences between _ dict _ and dir:

Dir () is a function that returns list;

_ Dict _ is a dictionary. The key is the attribute name and the value is the attribute value;

Dir () is used to find all the attributes of an object, including the attributes in _ dict _, __dict _ is a subset of dir;

Not all objects have the _ dict _ attribute. Many built-in types do not have the _ dict _ attribute, such as list. In this case, you need to use dir () to list all the attributes of an object.

_ Dict _ attributes

_ Dict _ is a dictionary used to store object attributes. Its key is the attribute name and its value is the attribute value.

#! /Usr/bin/python #-*-coding: UTF-8-*-class A (object): class_var = 1 def _ init _ (self): self. name = 'xy' self. age = 2 @ property def num (self): return self. age + 10 def fun (self): pass def static_f (): pass def class_f (cls): passif _ name _ = '_ main __': # main program a = A () print. _ dict _ # {'age': 2, 'name': 'xy'} the _ dict _ attribute print A in the instance. _ dict _ ''' _ dict _ attribute of Class A {'_ dict __': <attribute '_ dict _' of 'A' objects>, # For details, see the following link __': '_ main _', # module 'num': <property object>, # feature object 'class _ F': <function class_f>, # class method 'static _ F': <function static_f>, # static method 'class _ var': 1, 'fun ': <function fun>, # class variable '_ weakref _': <attribute '_ weakref _' of 'A' objects>, '_ doc _': None, # class description string '_ init _': <function _ init _ at 0x0000000003451AC8 >}'''. level1 = 3. fun = lambda: x print. _ dict _ # {'level1': 3, 'age': 2, 'name': 'xy', 'fun ': <function <lambda> at 0x >}print. _ dict _ # Same as the preceding result.. level2 = 4 print. _ dict _ # {'level1': 3, 'age': 2, 'name': 'xy'} print. _ dict _ # added the level2 attribute print object. _ dict _ ''' {'_ setattr _': <slot wrapper '_ setattr _' of 'object' objects>, '_ performance_ex _': <method '_ performance_ex _' of 'objects' objects>, '_ new __': <built-in method _ new _ of type object at>, and so on ..... '''

According to the above Code,

The _ dict _ of an instance only stores instance attributes related to the instance,

It is precisely because of the _ dict _ attribute of the instance that the attributes of each instance do not affect each other.

The _ dict _ class stores the variables and functions (class attributes, methods, etc.) shared by all instances. The _ dict _ class does not contain the attributes of its parent class.

Dir () function

Dir () is an API function provided by Python. the dir () function automatically searches for all attributes of an object (including attributes inherited from the parent class ).

The _ dict _ attribute of an instance is only a set of instance attributes and does not contain all valid attributes of the instance. Therefore, if you want to obtain all valid attributes of an object, you should use dir ().

Print dir (A) ''' ['_ class _', '_ delattr _', '_ dict __', '_ doc _', '_ format _', '_ getattribute _', '_ hash _', '_ init __', '_ module _', '_ new _', '_ reduce _', '_ performance_ex _', '_ repr __', '_ setattr _', '_ sizeof _', '_ str _', '_ subclasshook _', '_ weakref __', 'age', 'class _ F', 'class _ var', 'fun', 'level1', 'level2', 'name', 'num ', 'static _ F'] ''' a _ dict =. _ dict __. keys () A_dict =. _ dict __. keys () object_dict = object. _ dict __. keys () print a_dict print A_dict print object_dict ''['fun ', 'level1', 'age', 'name'] [' _ module __', 'level2', 'num', 'static _ F', '_ dict _', '_ weakref _', '_ init __', 'class _ F', 'class _ var', 'fun ',' _ doc _ '] [' _ setattr __', '_ performance_ex _', '_ new _', '_ reduce _', '_ str _', '_ format __', '_ getattribute _', '_ class _', '_ delattr _', '_ subclasshook _', '_ repr __', '_ hash _', '_ sizeof _', '_ doc __', '_ init _'] ''' # Because each class has a _ doc _ attribute, deduplication is required, after deduplication, compare print set (dir (a) = set (a_dict + A_dict + object_dict) # True

Conclusion

The dir () function automatically finds all the attributes of an object, including the attributes in _ dict.

_ Dict _ is a subset of dir (). dir () contains the attributes in _ dict.

The difference between the above article based on Python _ dict _ and dir () is all the content that I have shared with you. I hope you can provide a reference and support for the customer's house.

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.