2

For the attributes of a class:

  • The attribute __dict__ of a class or an instance doesn't include __base__, __name__. What attributes does __dict__ of a class contain, and what doesn't?
  • How can I get all the attributes of a class?

For the attributes of an instance:

  • The attribute __dict__ of an instance doesn't include __class__. What attributes does __dict__ of an instance contain, and what doesn't?
  • How can I get all the attributes of an instance?

Thanks.

  • Look at the [`dir`](https://docs.python.org/3/library/functions.html#dir) function. It includes parameters if super classes. More info [here](https://stackoverflow.com/questions/6761106/inspect-getmembers-vs-dict-items-vs-dir) – Paul Rooney Sep 06 '17 at 01:44
  • It doesn't include most of the magic methods, anything defined in `__slots__`, among other things. – Mad Physicist Sep 06 '17 at 02:12
  • @MadPhysicist Thanks. What does "magic methods" mean? –  Sep 06 '17 at 02:20
  • @Ben. Anything starting and ending with double underscores: `__init__`, `__new__`, `__eq__`, etc. These methods serve a special purpose in Python, you should not make new ones up, and most of them are not set in `__dict__` as far as I am aware. – Mad Physicist Sep 06 '17 at 02:25
  • 1
    Also, you can have an infinite number of accessible attributes via `__getattr__` and `__getattribute__`, mostly the latter. – Mad Physicist Sep 06 '17 at 02:26

2 Answers2

0

__dict__ excludes:

  • Any attributes (or descriptors) defined on the type (or any type in the mro).
  • Any attributes declared as part of a type implemented in C.
  • Any attributes declared in __slots__.

The best you can do for listing all attributes is dir, but it is often unreliable.

o11c
  • 15,265
  • 4
  • 50
  • 75
  • There are also, ways to override `__dict__` itself, and ways to override the overriding. I forget how this works. – o11c Sep 06 '17 at 03:43
-2

dict is interesting. What you need to understand is how Python looks up attributes, methods, etc. Thing like name are stored in dir(instance), which is not part of the standard getattr of your class. Only explicitly defined attributes and methods are part of A.dict, the other stuff is part of a complex look-up system, the rest is looking at the class, at the subclasses, etc, until you get to 'object'.

Connor Farrell
  • 107
  • 1
  • 3
  • Both second questions are not answered; and plz check the format `__dict__` vs __dict__ – InQβ Sep 06 '17 at 01:58