Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I override a parent class's functions in python?

I have a private method def __pickSide(self): in a parent class that I would like to override in the child class. However, the child class still calls the inherited def __pickSide(self):. How can I override the function? The child class's function name is exactly the same as the parent's function name.

like image 590
wrongusername Avatar asked Mar 20 '10 18:03

wrongusername


People also ask

Can you override functions in Python?

Method overriding in Python is when you have two methods with the same name that each perform different tasks. This is an important feature of inheritance in Python. In method overriding, the child class can change its functions that are defined by its ancestral classes.

Can we override parent class method?

In Java, method overriding occurs when a subclass (child class) has the same method as the parent class. In other words, method overriding occurs when a subclass provides a particular implementation of a method declared by one of its parent classes.

How do you override a subclass method?

Instance Methods The ability of a subclass to override a method allows a class to inherit from a superclass whose behavior is "close enough" and then to modify behavior as needed. The overriding method has the same name, number and type of parameters, and return type as the method that it overrides.

Can child class override the properties of parent class in Python?

If you add a method in the child class with the same name as a function in the parent class, the inheritance of the parent method will be overridden.

How to call parent class method after method overriding in Python?

Calling Parent class method after method overriding Using Classname: Parent’s class methods can be called by using the Parent classname.method inside the overridden method. Using Super (): Python super () function provides us the facility to refer to the parent class explicitly. It is...

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.

What is a parent class in Python?

To understand about the concept of parent class, you have to know about Inheritance in Python. In simpler terms, inheritance is the concept by which one class (commonly known as child class or sub class) inherits the properties from another class (commonly known as Parent class or super class).

What happens when you add a method to a parent class?

If you add a method in the child class with the same name as a function in the parent class, the inheritance of the parent method will be overridden.


2 Answers

Let's look at the easiest example:

from dis import dis

class A(object):
  def __pick(self):
      print "1"

  def doitinA(self):
      self.__pick()

class B(A):
  def __pick(self):
      print "2"

  def doitinB(self):
      self.__pick()

b = B()
b.doitinA() # prints 1
b.doitinB() # prints 2

dis(A.doitinA)
print
dis(B.doitinB)

The disassembly is as follows:

  8           0 LOAD_FAST                0 (self)
              3 LOAD_ATTR                0 (_A__pick)
              6 CALL_FUNCTION            0
              9 POP_TOP
             10 LOAD_CONST               0 (None)
             13 RETURN_VALUE

 15           0 LOAD_FAST                0 (self)
              3 LOAD_ATTR                0 (_B__pick)
              6 CALL_FUNCTION            0
              9 POP_TOP
             10 LOAD_CONST               0 (None)
             13 RETURN_VALUE

As you can see, Python mangles function names that begin with two underscores (and accesses to such names!!) to a name that includes the class name - in this case _A__pick and _B__pick). That means that the class in which a function is defined determines which of the __pick methods is called.

The solution is simple, avoid pseudo-private methods by removing the double underscores. For example, use _pick instead of __pick.

like image 187
AndiDog Avatar answered Oct 19 '22 11:10

AndiDog


The problem you're seeing is that the double underscores mangle the function name even in calls. This prevents polymorphism from working properly since the name it is mangled to is based on the name of the class the method is defined in, and not the name of the class of the object that is being referenced. Replacing the double underscores with something else will solve this.

like image 21
Ignacio Vazquez-Abrams Avatar answered Oct 19 '22 11:10

Ignacio Vazquez-Abrams