Is it possible to dynamically create/set variables within a subclass within the base class across multiple subclasses without affecting the other subclasses?
For example, take this code here:
class Base:
@classmethod
def __init__(cls,v):
cls.v=v
class sub1(Base):
Base.__init__(1)
class sub2(Base):
Base.__init__(5)
In that code, when sub1
is created, its v
attribute is equal to 1
. But when sub2
is created, the v
attribute of both sub1
and sub2
becomes 5
. I think I might know why this is. I assume the @classmethod
of the base class is not actually setting the attribute of the subclass, but instead its own attribute. Then that attribute is inherited in the subclasses. My question is: how can I use this kind of inheritance to set attributes of subclasses, not attributes of the base class which are inherited.
In other words, I would like to be able to use a similar structure, if possible (or at least a simple one) in order to accomplish setting attributes in subclasses that are specific to the subclasses and are not simply globally inherited from the base class.
Is this even possible?