Some Thoughts on Python caused by _ dict _ and dir (), python _ dict _

Source: Internet
Author: User

Some Thoughts on Python caused by _ dict _ and dir (), python _ dict _

For the differences and functions between _ dict _ and dir (), refer to this article:

Differences between Python _ dict _ and dir ()

Let's talk about the problems I encountered:

class Demo:  def __init__(self, name, age):    self.name = name    self.age = age  def func(self):    print('Hello {0}'.format(self.name))>>> d1 = Demo('Pythoner', 24)>>> hasattr(d1, 'func')True>>> d1.__dict__{'age': 24, 'name': 'Pythoner'}>>dir(d1)[ 'age', 'func', 'name','__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__']

First, we know that the instance method can also be regarded as an attribute and can be verified through the hasattr () function. _ dict _ is a dictionary used to store object attributes, but its return value does not contain 'function '!

Looking at the dir () function, it will automatically find all the attributes of an object (including attributes inherited from the parent class), and its return value contains 'func '.

Therefore, I guess that the "instance method" does not belong to the "private" attribute of the instance, but is shared by all instances of this class!

An instance requires a "private" process to obtain private properties, just like the _ init _ initialization function!

Verification:

class Demo2:  def __init__(self, name):    self.name = name  def func(self):    print('----get arg country----')    self.country = 'China'>>> d2 = Demo2('Pythoner')>>> d2.__dict__{'name': 'Pythoner'}>>> d2.func()----get arg country---->>> d2.__dict__{'country': 'China', 'name': 'Pythoner'}

The reason why "instance method" is called an instance method, or when each instance executes an instance method, different results are generated for different private attributes.SelfParameters.

When an instance executes an instance method, it searches for the method in the class to which it belongs, and thenSelfThe parameter passes the instance itself in, and the private attributes of the instance are passed together.SelfThe instance and method are bound.

Summary

The above is all the things about Python caused by _ dict _ and dir (). I hope it will be helpful to you. If you are interested, refer to this site: 3 errors to be avoided when using Python variables, * repeated operators in Python, etc. If you have any shortcomings, please leave a message.

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.