Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding "+=" in Python? (__iadd__() method)

Is it possible to override += in Python?

like image 803
Evan Fosmark Avatar asked Jun 26 '09 02:06

Evan Fosmark


People also ask

What is method of overriding 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 overriding class 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.

What is __ IADD __ in Python?

iadd() :- This function is used to assign and add the current value. This operation does “a+=b” operation. Assigning is not performed in case of immutable containers, such as strings, numbers and tuples.


1 Answers

Yes, override the __iadd__ method. Example:

def __iadd__(self, other):     self.number += other.number     return self     
like image 53
John Kugelman Avatar answered Sep 23 '22 03:09

John Kugelman