P
Paul Moore
I want to set up an inheritance hierarchy. The base class will define
a string value which should include the class name, but I don't want
people who inherit from my class to have to remember to override the
value.
If I do this using an instance variable, it's reasonably easy:
.... def __init__(self):
.... self.key = 'Key_for_' + self.__class__.__name__
.... def display(self):
.... print self.key
........ pass
....Key_for_Inherited
Rather than having the key for every instance, I'd like to use a class
variable, but I can't see how I'd make that work (a class variable
which is inherited but has a different value in derived classes). I
could use a classmethod,but that feels like even more overkill than an
instance attribute.
Is there a way of doing this via class variables or something, or more
relevantly, I guess, what would be the idiomatic way of doing
something like this?
Thanks,
Paul
a string value which should include the class name, but I don't want
people who inherit from my class to have to remember to override the
value.
If I do this using an instance variable, it's reasonably easy:
.... def __init__(self):
.... self.key = 'Key_for_' + self.__class__.__name__
.... def display(self):
.... print self.key
........ pass
....Key_for_Inherited
Rather than having the key for every instance, I'd like to use a class
variable, but I can't see how I'd make that work (a class variable
which is inherited but has a different value in derived classes). I
could use a classmethod,but that feels like even more overkill than an
instance attribute.
Is there a way of doing this via class variables or something, or more
relevantly, I guess, what would be the idiomatic way of doing
something like this?
Thanks,
Paul