[Tutor] How to get all methods of "object"?

Cameron Simpson cs at cskk.id.au
Sat Sep 21 22:35:24 EDT 2019


On 22Sep2019 12:12, DL Neil <PyTutor at danceswithmice.info> wrote:
>On 22/09/19 11:29 AM, boB Stepp wrote:
>>Python 3.7.4
>>I tried to do this with:
>>>>>for attribute in dir(object):
>>     print(attribute)
>...
>
>>But I know at least one that is not listed:  __dict__

"object" doesn't have a __dict__. Saves space!

>>And I would not
>>be surprised if there are others.

Really, Python tries to be very transparent here. If it isn't listed, it 
probably isn't a direct attribute of the class.

OTOH, the documentation for dir() includes this text:

  Because dir() is supplied primarily as a convenience for use at an 
  interactive prompt, it tries to supply an interesting set of names 
  more than it tries to supply a rigorously or consistently defined set 
  of names

In fact the doco for the dir function says a fair bit, while promising 
less that you might hope.

>>Basically, I am wondering what are *all* the things inherited from
>>"object" when one creates one's own class?

Yopu're probably doing the right thing. In the general case you probably 
need to consult every class in the subclass' __mro__ attribute. And 
instead of dir() you might consult the __dict__ or __slots__ attributes; 
note that __getattr__ lets a class offer attributes-on-demand which 
won't show in __dict__ or __slots__.

>>I am currently trying to find such a list in the Python 3 docs, but so
>>far I have been unsuccessful.

Also see the "inspect" module.

>An interesting idea...
>Try creating a class (which inherits the basic "object"), then __dict__ 
>(and two other 'extra' attributes) will become visible/pertinent.
>
>By definition, doesn't "inheritance" mean that the sub-class includes 
>*all* of the attributes (data and code) of its super-class(es)?

Unless overridden, yeah. But they don't all 

>(I'm not mentioning "slots" in a bid to preserve @boB's sanity)

Too late! In both senses :-)

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Tutor mailing list