2

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
4
  • 3
    What is "Animation"? You're going to have to provide more information, because the behavior you're describing depends completely on your class definition and not on Python's syntax. Commented Nov 1, 2010 at 13:41
  • Animation is a predefined class in an API. Commented Nov 1, 2010 at 13:50
  • Personally I think hacking __add__ to make + do something like this is a horrible abuse. Commented Nov 1, 2010 at 14:21
  • 1
    "anim1 gets executed after anim2" That's not really syntax. That's semantics. The question is "what does + mean"? Not "how is + parsed"? Commented Nov 1, 2010 at 15:51

3 Answers 3

8

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.

Sign up to request clarification or add additional context in comments.

1 Comment

It might even call anim2.__radd__(anim1) although that's very unlikely in this case.
3

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.