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:

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 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

class SpecialOptionsMenu(OptionsMenu)

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

def cap1(): return ['a', 'b', 'c']

def cap2(): return cap1() + ['d']

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