Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Method overriding, does signature matter?

Lets say I have

class Super():   def method1():     pass  class Sub(Super):   def method1(param1, param2, param3):       stuff 

Is this correct? Will calls to method1 always go to the sub class? My plan is to have 2 sub classes each override method1 with different params

like image 525
asdasasdsa Avatar asked May 17 '11 17:05

asdasasdsa


People also ask

Can we override method with different signature?

No, while overriding a method of the super class we need to make sure that both methods have same name, same parameters and, same return type else they both will be treated as different methods.

What is signature in method overriding?

Having two or more methods with the same name and signature as method in the parent class is known as method overriding. Method overriding allows us to invoke functions from base class to derived class.

What is not true about overriding in Python?

Question 4: What is not true about overriding in Python? Redefining a base class method in the inherited class is called method overriding. Overriding is the essential feature of object-oriented language. The overridden methods must have the same number of arguments as the base class method of the same name.

How do you override a method in Python?

In Python method overriding occurs by simply defining in the child class a method with the same name of a method in the parent class. When you define a method in the object you make this latter able to satisfy that method call, so the implementations of its ancestors do not come in play.


1 Answers

In Python, methods are just key-value pairs in the dictionary attached to the class. When you are deriving a class from a base class, you are essentially saying that method name will be looked into first derived class dictionary and then in the base class dictionary. In order to "override" a method, you simply re-declare the method in the derived class.

So, what if you change the signature of the overridden method in the derived class? Everything works correctly if the call is on the derived instance but if you make the call on the base instance, you will get an error because the base class uses a different signature for that same method name.

There are however frequent scenarios where you want derived class method have additional parameters and you want method call work without error on base as well. This is called "Liskov substitution principle" (or LSP) which guarantees that if person switches from base to derived instance or vice versa, they don't have to revamp their code. To do this in Python, you need to design your base class with the following technique:

class Base:     # simply allow additional args in base class     def hello(self, name, *args, **kwargs):         print("Hello", name)  class Derived(Base):       # derived class also has unused optional args so people can       # derive new class from this class as well while maintaining LSP       def hello(self, name, age=None, *args, **kwargs):           super(Derived, self).hello(name, age, *args, **kwargs)            print('Your age is ', age)  b = Base() d = Derived()  b.hello('Alice')        # works on base, without additional params b.hello('Bob', age=24)  # works on base, with additional params d.hello('Rick')         # works on derived, without additional params d.hello('John', age=30) # works on derived, with additional params 

Above will print:

     Hello Alice     Hello Bob     Hello Rick     Your age is  None     Hello John     Your age is  30 
. Play with this code
like image 55
Shital Shah Avatar answered Sep 18 '22 20:09

Shital Shah