简体   繁体   English

Python如何评估这个表达式?

[英]How does Python evaluate this expression?

How does Python evaluate the following expression? Python如何评估以下表达式? anim1 gets executed after anim2 . anim1anim2之后执行。 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) . 这将调用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. 为了理解__add__发生的事情,你必须从Animation类检查__add__方法的定义。

In Python, you can redefine the behavior of the mathematical operators. 在Python中,您可以重新定义数学运算符的行为。 If I understood your question, Animation probably redefines the "+" operator using the __add__ method. 如果我理解你的问题,动画可能会使用__add__方法重新定义“+”运算符。

More info: Official Documentation 更多信息: 官方文档

Check out the dis module . 查看dis模块 It has a function dis that will take a function/module/class and show you the byte code. 它有一个函数dis ,它将采用函数/模块/类并显示字节代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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