Skip to content Skip to sidebar Skip to footer

Python - Multiple Inheritance With Same Name

I have main class as: class OptionsMenu(object): def __init__(self, name): try: self.menu = getattr(self, name)() except: self.menu = N

Solution 1:

Nothing stops you from getting result of parent's static function:

classOptionsMenu:
    @staticmethod
    def cap():
        return ['a', 'b', 'c']


classOptionsMenu(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 as noted to give same class names for different classes is very bad idea! It will lead to many problems in future.

Try to think, why do you want same names for different classes. May be they should have different names?

class OptionsMenu

classSpecialOptionsMenu(OptionsMenu)

Or may be as soon as cap() is static method, make different functions out of OptionsMenu?

defcap1(): return ['a', 'b', 'c']

defcap2(): return cap1() + ['d']

Post a Comment for "Python - Multiple Inheritance With Same Name"