如何在python中重命名超类的方法?

Pro*_*o Q 3 python methods class subclass superclass

我有一个超类使用该方法run().

我创建了一个超类的子类,我想拥有自己的run()方法.但是,我想在一个oldrun()在这个新对象上调用的方法中保留旧run方法的功能.

我将如何在Python中执行此操作?

Bre*_*arn 6

你可以这样做:

class Base(object):
    def run(self):
        print("Base is running")

class Derived(Base):
    def run(self):
        print("Derived is running")

    def oldrun(self):
        super().run()
Run Code Online (Sandbox Code Playgroud)