python - 将代码 append 到继承的类方法

标签 python class append

您将如何 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

Learn more about super here

关于python - 将代码 append 到继承的类方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28549240/

相关文章:

python - 如何将 qss 应用于 QCalendarWidget?

java - Java中的抽象变量?

Javascript 通过变量命名类

javascript - 如何在 JavaScript 中将一个变量 append 到另一个变量?

python - 如何检查 numpy 数组是否为空?

python - 等效于 Python 的 OpenCV 中的 FileNotFoundError

java - 从另一个文件中更改文件中的特定行

matlab - 将元素 append 到数组的最快方法是什么?

swift - 在对象中 append 时避免值相乘

python - 使用 Pandas 操作数据框,创建新列并根据在数据框中查找现有数据来填充它们的值