I have defined a base class somewhere, that is extendable by some user.
Now i want to access variables potentially defined by that user in his subclasses so that i can do something with it.
My attempt to solve this problem is by using metaclass
es
Here's the example set up:
class Meta(type):
def __new__(cls, name, bases, attrs):
clsobj = super().__new__(cls, name, bases, attrs)
print(f'the name of this class is {clsobj.NAME.upper()}')
return clsobj
class Base(metaclass=Meta):
pass
class C1(Base):
NAME = "C1"
if __name__ == "__main__":
c1 = C1()
But i'm getting the following error:
AttributeError: type object 'Base' has no attribute 'NAME'
N.B. The Base
class shouldn't neither know about its sublcass variables, but should just access them.