Subclasses of object class for Python in Linux command line


A Python install on Linux identifies 187 subclasses of the object class through this query:

[email protected]:~$ python3 -c 'print(len("".__class__.__mro__[1].__subclasses__()))'

The same query in an IDLE shell identifies 342 subclasses, and in the IDE PyCharm only 217!

Can the user decide which subclasses are operative in a given environment, or are subclasses rigorously enforced by the vendor?


When performing the check on various setups it seems your query is based on properties of the object class:

(<class 'str'>, <class 'object'>)

Thus your query can be translated as:


Let’s rephrase your question as: Why does the count of subclasses of the class object vary with respect to the context?

The key is: Your query literally counts the number of classes inheriting from the class object (read more about this specific class) at the moment of querying and this count definitely depends on your version of python, environment and imports.

For sure initialization of python in both cases are not equivalent as the number of objects inheriting from object are different.

For instance, defining a class depending – explicitly or not – on object is sufficient to change this count:

>>> len(object.__subclasses__())
>>> class A(object):
...    pass
>>> len(object.__subclasses__())
>>> class B:
...     pass
>>> len(object.__subclasses__())

Importing a package will indeed change this count as well:

>>> import pandas as pd
>>> len(object.__subclasses__())

Notice that it also depends on the environment. So basically it depends on the PYTHONPATH and installed modules that can be found in this path:

> activate base
(base) > python -c "print(len(object.__subclasses__()))"
(base) > activate test
(test) > python -c "print(len(object.__subclasses__()))"

So the answer is almost certainly because the IDLE your ran is not the same python version or it have not the same environment or it has different modules imported than the PyCharm IDE.

As pointed by @jasonharper IDLE might have imported python classes that does increase the count.

So of you need to keep this check you will have to fix the context to make it reproducible. You can use environments or containers with specific images for that. Or design another check that is more resilient.

Answered By – jlandercy

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published