global variable not working inside function. Increment

H

Helvin

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
 
R

Rami Chowdhury

global no_picked
no_picked = 0

def picked(object, event):
no_picked += 1
print no_picked

In order to be able to affect variables in the global scope, you need to
declare them global inside the function, and not at the global scope. So
your code should read:

no_picked = 0

def picked(object, event):
global no_picked
no_picked += 1
print no_picked

I believe that will work.
 
F

feather.duster.kung.fu

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
 
C

charles benoit

In order to be able to affect variables in the global scope, you need to
declare them global inside the function, and not at the global scope. So
your code should read:

no_picked = 0

def picked(object, event):
global no_picked
no_picked += 1
print no_picked

I believe that will work.





--
Rami Chowdhury
"Never attribute to malice that which can be attributed to stupidity" --
Hanlon's Razor
408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)
 
F

feather.duster.kung.fu

Thank You for setting that straight. I'm just learning Python and NONE of the tutorials I read said anything about that . In fact they all say a global can be called from inside a Function. If possible please contact the ppl that write these things.....I've heard of Ocam's razor but not Hanlon's???
 
A

Andreas Perstinger

feather.duster.kung.fu said:
I'm just learning Python and NONE of the tutorials I read said
anything about that . In fact they all say a global can be called from
inside a Function. If possible please contact the ppl that write these
things.

Well, we don't know which tutorials you read.
So why don't you tell them yourself?

Bye, Andreas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top