inconsistency with += between different types ?

Christopher A. Craig list-python at ccraig.org
Wed Aug 7 15:59:16 EDT 2002


Andreas.Leitgeb at siemens.at (Andreas Leitgeb) writes:

> > So what? Immutable objects don't have to implement __iadd__ et al. at
> > all and in fact ints don't have __iadd__. Python falls back to calling
> > __add__ and normal assignment automatically.
> 
> That's exactly what I meant.
> For immutable objects, __iadd__ et al. simply make no sense (IMHO).
> 

Now that I understand your problem, I would not oppose this solution.
(I wouldn't really support it either because I don't actually see a
problem since I have yet to see someone actually try to define an
__iadd__ that doesn't return self.)  My confusion came in that I
recalled back to your original post (quoted below) and expected that
you were trying to fix that, which this doesn't since neither integers
nor strings define __iadd__:

> --- snip ---
> def f(x,y): x+=y
> 
> d='foo';     f(d,'bar');   print d    #  ->  'foo'
> i=4;         f(i,2);       print i    #  ->  4
> #but:
> l=[1,2,3];   f(l,[7,8,9]); print l    #  ->  [1, 2, 3, 7, 8, 9]
> --- snip ---
>  
> It seems that  lists are the only type, where the changes get visible
> outside, although I'd have expected all types to behave as lists do.


-- 
Christopher A. Craig <list-python at ccraig.org>
"May the source be with you."  -- Open Source Software mantra




More information about the Python-list mailing list