Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Python evaluate this expression?

Tags:

python

syntax

How does Python evaluate the following expression? anim1 gets executed after anim2. How does a simple + operator that?

anim1 = Animation(duration=1, center=(100,100) type='delta')
anim2 = Animation(duration=1, rotation=45 type='delta')

anim = anim1 + anim2
like image 861
Gooner Avatar asked Jul 24 '26 12:07

Gooner


2 Answers

This will call anim1.__add__(anim2).

In order to understand what is happening under the hood you have to inspect the definition of __add__ method from Animation class.

like image 106
Paulo Scardine Avatar answered Jul 27 '26 00:07

Paulo Scardine


In Python, you can redefine the behavior of the mathematical operators. If I understood your question, Animation probably redefines the "+" operator using the __add__ method.

More info: Official Documentation

like image 22
Marc Demierre Avatar answered Jul 27 '26 02:07

Marc Demierre