Python Source Code Beautifier

Delaney, Timothy (Tim) tdelaney at avaya.com
Wed Feb 28 16:40:13 EST 2007


Alan Franzoni wrote:

> Yeah, that's right, it could have semantic differences, but that
> shouldn't be the case anyway. I mean, if I don't define an __iadd__
> method, writing 
> 
> a += n
> 
> or
> 
> a = a + n
> 
> is just the same, right?
> 
> So, if I bother to define an __iadd__ method, I should make sure it
> works just the same, or I would introduce a very strange and
> hard-to-understand behaviour.

As you've seen, that's not the case. The *whole point* of having
__iadd__ is to perform an *in-place* add i.e. mutate the object you're
assigning to.

So it's the behaviour of *immutable* objects that is the exception. The
other semantics of immutable objects cover that though - you should
never rely on whether two equal immutable objects (like integers) are
the same or different objects.

Tim Delaney



More information about the Python-list mailing list