best way to increment an IntVar?
Dave Angel
davea at ieee.org
Fri Jun 25 18:23:03 EDT 2010
Alan G Isaac wrote:
> On 6/25/2010 3:52 PM, Dave Angel wrote:
>> I said "default", not "only" behavior. I suspect list provides an
>> __iadd__ method to provide this ability. Integers do not, and
>> therefore neither does the object the OP was asking about.
>
>
> I have no idea what "default behavior" is supposed to mean.
> Mutable objects like a list will generally modify in place.
> Immutable objects of course will not. An IntVar is mutable.
> You have given no reason for it not to handle ``+=`` in an
> unsurprising fashion. It is not an int.
>
> Alan Isaac
>
>
To quote the docs,
"If a specific method is not defined, the augmented assignment falls
back to the normal methods. For instance, to execute the statement x +=
y, where /x/ is an instance of a class that has an __iadd__()
<#object.__iadd__> method, x.__iadd__(y) is called. If /x/ is an
instance of a class that does not define a __iadd__() <#object.__iadd__>
method, x.__add__(y) and y.__radd__(x) are considered, as with the
evaluation of x + y."
In other words, if a class defines __add(), but not __iadd__(), then
it'll behave this way. That's what I mean by default.
I didn't say it should, merely that it does. I suspect that __iadd__
didn't exist when IntVar was being defined, and nobody got around to
adding it.
Another part of the docs would seem to encourage IntVar to change:
"An augmented assignment expression like x += 1 can be rewritten as x =
x + 1 to achieve a similar, but not exactly equal effect. In the
augmented version, x is only evaluated once. Also, when possible, the
actual operation is performed /in-place/, meaning that rather than
creating a new object and assigning that to the target, the old object
is modified instead"
DaveA
More information about the Python-list
mailing list