i

Python Programming

Method overriding

Suppose Vector class to represent two-dimensional vectors, what happens when you use the plus operator to add them? Python will yell at you.

You could define the __add__ method in your class to perform vector addition and then plus operator would behave as per the expectation:

Example

Live Demo

Output:

vector(7,8)