Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 875
Pro Q Avatar asked Dec 20 '16 06:12

Pro Q


People also ask

How do you rename a method?

Create a new method with a new name. Copy the code of the old method to it. Delete all the code in the old method and, instead of it, insert a call for the new method. Find all references to the old method and replace them with references to the new one.

How do you rename a function in Python?

The only way to rename a function is to change the code .

How do you call a super function in Python?

method inside the overridden method. Using Super(): Python super() function provides us the facility to refer to the parent class explicitly. It is basically useful where we have to call superclass functions. It returns the proxy object that allows us to refer parent class by 'super'.

What is super () __ Init__ in Python?

The “__init__” is a reserved method in python classes. It is known as a constructor in Object-Oriented terminology. This method when called, allows the class to initialize the attributes of the class. Python super() The super() function allows us to avoid using the base class name explicitly.


1 Answers

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()
like image 51
BrenBarn Avatar answered Oct 22 '22 18:10

BrenBarn