__iadd__ and fellows missing (Python 2.2)
Ralf Juengling
juenglin at informatik.uni-freiburg.de
Sat Apr 13 06:27:20 EDT 2002
Michael Hudson <mwh at python.net> writes:
> Ralf Juengling <juenglin at informatik.uni-freiburg.de> writes:
>
> > Shouldn't I find the special methods '__iadd__' et al, when
> > asking for the attributes of 'int'?
>
> ints are immutable, so they don't implement __iadd__. lists do:
>
Hm. But then an expression 'i+=1' should cause an error if 'i'
is an int...
>>> i = 1
>>> type(i)
<type 'int'>
>>> id(i)
134526912
>>> i+=1
>>> id(i)
134526852
>>>
I understand now, that 'i+=1' is no real in-place operation but
just a shortcut for 'i=i+1' since int is immutable. The language
reference states that this is okay, anyhow, I find it confusing.
For the sake of clarity, an 'in-place' operation should really
work in-place, don't you think?
Cheers,
Ralf
> > I couldn't find them either in ther operator-module:
>
> Hmm, I think the issue here is that
>
> x += y
>
> translates (in the presence of an __iadd__ method) roughly to
>
> x = x.__iadd__(x, y)
>
> and you can't do that in the form of
>
> operator.blah(x,y)
>
> . Or it might just be oversight.
>
> Cheers,
> M.
>
> --
> Remember - if all you have is an axe, every problem looks
> like hours of fun. -- Frossie
> -- http://home.xnet.com/~raven/Sysadmin/ASR.Quotes.html
More information about the Python-list
mailing list