global variable not working inside function. Increment

feather.duster.kung.fu at gmail.com feather.duster.kung.fu at gmail.com
Mon May 13 10:02:28 EDT 2013


On Friday, September 4, 2009 4:43:27 PM UTC-7, Helvin wrote:
> Hi,
> 
> This increment thing is driving me nearly to the nuts-stage. > <
> 
> I have a function that allows me to pick points. I want to count the
> number of times I have picked points.
> 
>     global no_picked
>     no_picked = 0
> 
>     def picked(object, event):
>           no_picked += 1
>           print no_picked
> 
> Error msg says: UnboundLocalError: local variable 'no_picked'
> referenced before assignment
> For some reason, no_picked does not increment, but the printing
> statement works.
> 
> Do you know why?
> 
> (I'm actually writing this for a vtkrenderwindowinteractor.)
> 
> Helvin




More information about the Python-list mailing list