7

I'm new to Python so apologies in advance if this is a stupid question.

For an assignment I need to overload augmented arithmetic assignments(+=, -=, /=, *=, **=, %=) for a class myInt. I checked the Python documentation and this is what I came up with:

def __iadd__(self, other):

    if isinstance(other, myInt):
        self.a += other.a
    elif type(other) == int:
        self.a += other
    else:
        raise Exception("invalid argument")

self.a and other.a refer to the int stored in each class instance. I tried testing this out as follows, but each time I get 'None' instead of the expected value 5:

c = myInt(2)
b = myInt(3)
c += b
print c

Can anyone tell me why this is happening? Thanks in advance.

1
  • 2
    I believe there are no stupid questions.. Commented Feb 15, 2010 at 17:22

3 Answers 3

14

You need to add return self to your method. Explanation:

The semantics of a += b, when type(a) has a special method __iadd__, are defined to be:

  a = a.__iadd__(b)

so if __iadd__ returns something different than self, that's what will be bound to name a after the operation. By missing a return statement, the method you posted is equivalent to one with return None.

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

1 Comment

Alex,I always love your answers!
7

Augmented operators in Python have to return the final value to be assigned to the name they are called on, usually (and in your case) self. Like all Python methods, missing a return statement implies returning None.

Also,

  • Never ever ever raise Exception, which is impossible to catch sanely. The code to do so would have to say except Exception, which will catch all exceptions. In this case you want ValueError or TypeError.
  • Don't typecheck with type(foo) == SomeType. In this (and virtually all) cases, isinstance works better or at least the same.
  • Whenever you make your own type, like myInt, you should name it with capital letters so people can recognize it as a class name.

Comments

1

Yes, you need "return self", it will look like this:

def __iadd__(self, other):
    if isinstance(other, myInt):
        self.a += other.a
        return self
    elif type(other) == int:
        self.a += other
        return self
    else:
        raise Exception("invalid argument")

1 Comment

Also, you should make your Exception a TypeError.

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.