您将如何 append 到继承对象的方法?比如说:
class ABeautifulClass(GoodClass):
def __init__(self, **kw):
# some code that will override inherited code
def aNewMethod(self):
# do something
现在我已经从 GoodClass
继承了代码,我该如何将代码 append 到继承的方法。如果我从 GoodClass
继承代码,我将如何 append 到它,而不是基本上删除它并重写它。这在 Python 中可行吗?
最佳答案
尝试使用 super
class ABeautifulClass(GoodClass):
def __init__(self, **kw):
# some code that will override inherited code
def aNewMethod(self):
ret_val = super().aNewMethod() #The return value of the inherited method, you can remove it if the method returns None
# do something
关于python - 将代码 append 到继承的类方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28549240/