Search code examples
pythonclassmethodssubclasssuperclass

How do I rename a superclass's method in python?


I have a superclass with the method run().

I make a subclass of the superclass that I would like to have its own run() method. But, I want to keep the functionality of the old run method in a method called oldrun() on this new object.

How would I go about doing this in Python?


Solution

  • You could do it like this:

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