i have main class as:
class optionsmenu(object): def __init__(self, name): try: self.menu = getattr(self, name)() except: self.menu = none @staticmethod def cap(): return ['a', 'b']
and have child class override as:
class optionsmenu(optionsmenu): @staticmethod def cap(): return ['a', 'b', 'c']
the first class gives menu options default template, while next gives other template.
i want class optionsmenu
derived child class, , list (['a', 'b', 'c']
) , make changes that.
is achievable in python? if yes, how may achieve this?
many help!
nothing stops getting result of parent's static function:
class optionsmenu: @staticmethod def cap(): return ['a', 'b', 'c'] class optionsmenu(optionsmenu): @staticmethod def cap(): lst = super(optionsmenu, optionsmenu).cap() # ['a', 'b', 'c'] lst.append('d') return lst print(optionsmenu.cap()) # ['a', 'b', 'c', 'd']
but noted give same class names different classes is bad idea! lead many problems in future.
try think, why want same names different classes. may should have different names?
class optionsmenu class specialoptionsmenu(optionsmenu)
or may cap()
static method, make different functions out of optionsmenu
?
def cap1(): return ['a', 'b', 'c'] def cap2(): return cap1() + ['d']
Comments
Post a Comment