var or inout parm?
Hrvoje Niksic
hniksic at xemacs.org
Fri Dec 12 20:20:59 EST 2008
sturlamolden <sturlamolden at yahoo.no> writes:
> What? Take a look at the code again:
>
> mytuple[0] += 1
>
> should never attempt an __iadd__ on mytuple.
>
> A sane parser would see this as:
>
> tmp = mytuple.__getitem__(0)
> tmp = tmp.__iadd__(1)
> mytuple.__setitem__(0, tmp) # should this always raise an exception?
What do you mean by "a sane parser"? This is exactly what happens in
current Python. Since tuple's __setitem__ always raises an exception
(and that is by design and not likely to change), everything is sane.
Saner (in this respect) behavior in the tuple example would require a
different protocol. I don't understand why Python doesn't just call
__iadd__ for side effect if it exists. The decision to also rebind
the result of __i*__ methods continues to baffle me. I guess it is a
result of a desire to enable the __i*__ methods to punt and return a
different instance after all, but if so, that design decision brings
more problems than benefits.
More information about the Python-list
mailing list