Python inheritance - how to disable a function

前端 未结 7 875
离开以前
离开以前 2020-12-01 04:18

In C++ you can disable a function in parent\'s class by declaring it as private in the child class. How can this be done in Python? I.E. How can I hide parent\'s function fr

7条回答
  •  栀梦
    栀梦 (楼主)
    2020-12-01 04:58

    This is the cleanest way I know to do it.

    Override the methods and have each of the overridden methods call your disabledmethods() method. Like this:

    class Deck(list):
    ...
    @staticmethod
        def disabledmethods():
            raise Exception('Function Disabled')
        def pop(self): Deck.disabledmethods()
        def sort(self): Deck.disabledmethods()
        def reverse(self): Deck.disabledmethods()
        def __setitem__(self, loc, val): Deck.disabledmethods()
    

提交回复
热议问题