What is the difference between dir (self) and self .__ dict__

Possible duplicate:
inspect.getmembers () vs __dict __. items () vs dir ()

class Base(): def __init__(self, conf): self.__conf__ = conf for i in self.__dict__: print i,"dict" for i in dir(self): print i,"dir" class Test(Base): a = 1 b = 2 t = Test("conf") 

Output:

 __conf__ dict __conf__ dir __doc__ dir __init__ dir __module__ dir a dir b dir 

Can someone explain a little about this?

+4
source share
1 answer

The __dict__ object holds the attributes of the instance. The only attribute of your instance is __conf__ , since it is the only one set in your __init__() method. dir() returns a list of the names of the "interesting" attributes of an instance, its class, and its parent classes. These include a and b from your Test class and __init__ from your Base class, as well as several other attributes that Python automatically adds.

The class attributes are stored in each __dict__ class, so what dir() does is go through the inheritance hierarchy and collect information from each class.

+7
source

Source: https://habr.com/ru/post/1444992/


All Articles