Reputation: 31504
Python classes contain namespaces and the function dir
lists the content of the namespace.
The object
namespace contains several functions, for example the function __subclasses__
that, when invoked, returns a list containing all the known classes that are subclasses of object
.
The output of dir(object)
is:
['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__',
'__hash__', '__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
'__setattr__', '__sizeof__', '__str__', '__subclasshook__']
The __subclasses__
function is not listed, but it's there, try: object.__subclasses__()
.
I understand that a class can change the __dir__
dictionary to customize what dir
shows, but that's not the case of object
, in fact object.__dir__
raises an exception.
Still, when I use IPython autocompletion, the __subclasses__
function is there. How can I obtain the same list of elements that IPython uses so that I know that also __subclasses__
is there?
Upvotes: 2
Views: 16308
Reputation: 280887
IPython finds __subclasses__
through perfectly ordinary means, by looking at type(object)
. On the other hand, since object
is a type, dir
for types specifically does not look at type(object)
. Whoever wrote dir
decided seeing metaclass attributes in the list would be confusing.
Upvotes: 5