Skip to content
Advertisement

Why do I get False when using issubclass in this way?

Structure:

JavaScript

m1.py:

JavaScript

m2.py:

JavaScript

I don’t now why I get false while I think it’s obviously true when I run m1.py. My python version is python3.5.2.

Advertisement

Answer

Wellcome to the world of modules and namespaces!

Here is what happens:

In module m2, you import A from module m1. So you create a class m2.A as a reference to class m1.A. It happens to have the same definition as __main__.A, but they are different objects, because the main module is named __main__ and not m1. Then in module __main__ you create a class __main__.B as a reference to class m2.B

To better understand what happens here, I have added some code to m1:

JavaScript

The output is:

JavaScript

Proving that B is indeed a subclass of m1.A but not of __main__.A.

Advertisement