From: Alan G Isaac on
Of course one can do
myintvar.set(myintvar.get()+1)
but surely there is a better way?

I'm surprsied that
myintvar += 1
is not allowed.

Thanks,
Alan Isaac
From: Ben Finney on
Alan G Isaac <alan.isaac(a)gmail.com> writes:

> Of course one can do
> myintvar.set(myintvar.get()+1)
> but surely there is a better way?

What is an IntVar? It's not a built-in type, so you'll need to tell us.

type(myintvar)

Where is it imported from?

import sys
sys.modules[type(myintvar).__module__]

> I'm surprsied that
> myintvar += 1
> is not allowed.

Does the documentation lead you to believe it would be allowed?

help(type(myintvar))

--
\ “I cannot be angry at God, in whom I do not believe.” —Simone |
`\ De Beauvoir |
_o__) |
Ben Finney
From: rantingrick on
On Jun 24, 12:07 am, Ben Finney <ben+pyt...(a)benfinney.id.au> wrote:
> Alan G Isaac <alan.is...(a)gmail.com> writes:
>
> > Of course one can do
> >    myintvar.set(myintvar.get()+1)
> > but surely there is a better way?
>
> What is an IntVar? It's not a built-in type, so you'll need to tell us.

For those who don't know; IntVar is from the module Tkinter. It is a
class to contain an integer control var used to update Tkinter widgets
that support it.

@OP: No there is not an __iadd__ method in the IntVar class but if you
really, really want to update this way just subclass IntVar and create
the method yourself. OOP is cool like dat!

You can also configure a Tkiter widget via the Python syntax w[key] =
value OR w.config(key1=value1, key2=value2, ...)


From: Bruno Desthuilliers on
Dennis Lee Bieber a �crit :
(snip - about Tkinter IntVar type)

> It is NOT a numeric "variable" in Python realms.
>
> So var+=increment can't be used because Python would rebind the name
> var to a new object -- but Tkinter would still be hooked to the original
> object and never see the change.

>>> foo = []
>>> bar = foo
>>> foo += [1]
>>> foo
[1]
>>> bar
[1]
>>> bar is foo
True
>>> foo.__iadd__([2])
[1, 2]
>>> _ is foo
True
>>>

IOW : __iadd__ doesn't necessarily returns a new object !-)



From: Alan G Isaac on
On 6/24/2010 1:59 AM, Dennis Lee Bieber wrote:
> It is NOT a numeric "variable" in Python realms.

Sure, but why does it not behave more like one?
It seems both obvious and desirable, so I'm
guessing there is a good reason not to do it.

> So var+=increment can't be used because Python would rebind the name
> var to a new object

>>> import Tkinter as tk
>>>
>>> class IntVar2(tk.IntVar):
.... def __iadd__(self, val):
.... self.set(self.get()+val)
.... return self
....
>>> root = tk.Tk()
>>> myintvar2 = IntVar2()
>>> temp = myintvar2
>>> myintvar2 += 5
>>> print(myintvar2.get(),myintvar2 is temp)
(5, True)

Alan Isaac