简体   繁体   中英

How does Python evaluate this expression?

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

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.

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

Check out the dis module . It has a function dis that will take a function/module/class and show you the byte code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM