Suppose we have a base class, A
, that contains some class variables. This class also has a class method foo
that does something with those variables. Since this behavior shouldn't be hard-coded (e.g. we don't want to have to modify foo
when adding new class variables), foo
reads cls.__dict__
instead of directly referencing the variables.
Now we introduce a derived class: B
extends A
with some more class variables, as well as inherits foo
. Code example:
class A:
x = 0
y = 1
@classmethod
def foo(cls):
print([name for name, prop in cls.__dict__.items() if type(prop) is int])
class B(A):
z = 3
print(B.x) # prints "0"
A.foo() # prints "['x', 'y']"
B.foo() # prints "['z']" -- why not "['x', 'y', 'z']"?
Therefore, my question is: why B.__dict__
does not contain the variables inherited from A
, and if not there, then where are they?
This is not a duplicate of Accessing class attributes from parents in instance methods, because I don't just want to query specific variables that happen to be in the base class - I want to list them without knowing their names. The answers related to MRO given in this question might happen to also apply here, but the original problem is in my view different.