Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #45240 > unrolled thread

Re: global variable not working inside function. Increment

Started bycharles benoit <feather.duster.kung.fu@gmail.com>
First post2013-05-13 07:10 -0700
Last post2013-05-13 18:13 +0200
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: global variable not working inside function. Increment charles benoit <feather.duster.kung.fu@gmail.com> - 2013-05-13 07:10 -0700
    Re: global variable not working inside function. Increment "feather.duster.kung.fu" <feather.duster.kung.fu@gmail.com> - 2013-05-13 07:25 -0700
      Re: global variable not working inside function. Increment Andreas Perstinger <andipersti@gmail.com> - 2013-05-13 18:13 +0200

#45240 — Re: global variable not working inside function. Increment

Fromcharles benoit <feather.duster.kung.fu@gmail.com>
Date2013-05-13 07:10 -0700
SubjectRe: global variable not working inside function. Increment
Message-ID<4c334b0e-88ba-43b1-9201-7a2ef5a270fe@googlegroups.com>
On Friday, September 4, 2009 4:52:11 PM UTC-7, Rami Chowdhury wrote:
> >     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.
> 
> On Fri, 04 Sep 2009 16:43:27 -0700, Helvin <helvinlui@gmail.com> 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
> 
> 
> 
> -- 
> 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)

[toc] | [next] | [standalone]


#45241

From"feather.duster.kung.fu" <feather.duster.kung.fu@gmail.com>
Date2013-05-13 07:25 -0700
Message-ID<688b6146-4601-47a7-8418-5fbc28650307@googlegroups.com>
In reply to#45240
On Monday, May 13, 2013 7:10:50 AM UTC-7, charles benoit wrote:
> On Friday, September 4, 2009 4:52:11 PM UTC-7, Rami Chowdhury wrote:
> 
> > >     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.
> 
> > 
> 
> > On Fri, 04 Sep 2009 16:43:27 -0700, Helvin <helvinlui@gmail.com> 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
> 
> > 
> 
> > 
> 
> > 
> 
> > -- 
> 
> > 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)

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???

[toc] | [prev] | [next] | [standalone]


#45247

FromAndreas Perstinger <andipersti@gmail.com>
Date2013-05-13 18:13 +0200
Message-ID<mailman.1630.1368462095.3114.python-list@python.org>
In reply to#45241
"feather.duster.kung.fu" <feather.duster.kung.fu@gmail.com> wrote:
>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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web