Re: global/local variables

2008-01-25 Thread Tim Rau
On Jan 25, 5:31 am, Steven D'Aprano <[EMAIL PROTECTED] cybersource.com.au> wrote: > On Thu, 24 Jan 2008 23:04:42 -0800, Tim Rau wrote: > > UnboundLocalError: local variable 'nextID' referenced before assignment > > When you assign to a name in Python, the compiler treats it as a local > variable. S

Re: global/local variables

2008-01-25 Thread Steven D'Aprano
On Fri, 25 Jan 2008 03:28:05 -0800, Tim Rau wrote: >> Because you don't assign to allThings, and therefore it is treated as >> global. >> >> > Hmm so I can't assign to globals in a local environment? How do I > make it see that I'm assigning to a global? I thought somebody had already mentio

Re: global/local variables

2008-01-25 Thread Tim Rau
On Jan 25, 5:31 am, Steven D'Aprano <[EMAIL PROTECTED] cybersource.com.au> wrote: > On Thu, 24 Jan 2008 23:04:42 -0800, Tim Rau wrote: > > UnboundLocalError: local variable 'nextID' referenced before assignment > > When you assign to a name in Python, the compiler treats it as a local > variable. S

Re: global/local variables

2008-01-25 Thread Steven D'Aprano
On Thu, 24 Jan 2008 23:04:42 -0800, Tim Rau wrote: > UnboundLocalError: local variable 'nextID' referenced before assignment When you assign to a name in Python, the compiler treats it as a local variable. So when you have a line like this: nextID += 1 # equivalent to nextID = nextID + 1 you

Re: global/local variables

2008-01-25 Thread Helmut Jarausch
Tim Rau wrote: > I'm sorry: I forgot to say what my problem was. Python seems to think > that nextID is a local, and complains that it can't find it. THis is > not the full text of the function, just the level that is causing > errors. the lack of : on the if is a transcription error. > Traceback

Re: global/local variables

2008-01-24 Thread Tim Rau
On Jan 24, 7:09 pm, Steven D'Aprano <[EMAIL PROTECTED] cybersource.com.au> wrote: > On Thu, 24 Jan 2008 15:37:09 -0800, Tim Rau wrote: > > What makes python decide whether a particular variable > > is global or local? > > For starters, if the line of code is not inside a class or function, that > i

Re: global/local variables

2008-01-24 Thread Steven D'Aprano
On Thu, 24 Jan 2008 15:37:09 -0800, Tim Rau wrote: > What makes python decide whether a particular variable > is global or local? For starters, if the line of code is not inside a class or function, that is, it's at the top level of a module, it is global. More interesting is if it is inside a

Re: global/local variables

2008-01-24 Thread Diez B. Roggisch
Tim Rau schrieb: > What makes python decide whether a particular variable is global or > local? I've got a list and a integer, both defined at top level, no > indentation, right next to each other: > > allThings = [] > nextID = 0 > > and yet, in the middle of a function, python sees one and doesn

global/local variables

2008-01-24 Thread Tim Rau
What makes python decide whether a particular variable is global or local? I've got a list and a integer, both defined at top level, no indentation, right next to each other: allThings = [] nextID = 0 and yet, in the middle of a function, python sees one and doesn't see the other: class ship(thi

Re: NB question on global/local variables in functions

2006-07-17 Thread Bruno Desthuilliers
Wolfgang wrote: > Hi all, > > I've started to write some functions but I have some problems with > common variables in that functions. > > So I have some variables which should be accessible by all my functions > but not accessible by the rest of my code. How can I do this? You can use a closure

Re: NB question on global/local variables in functions

2006-07-15 Thread Fredrik Lundh
Wolfgang wrote: >> First, avoid "from function import *" as it pollutes your namespace. Either >> import specific symbols or just the module: >> >> from function import fun, fun1 >> import function > > thanks for the hint! But what is the difference between > from module import * >

Re: NB question on global/local variables in functions

2006-07-15 Thread Wolfgang
Thanks! I've learned a lot today! Wolfgang -- http://mail.python.org/mailman/listinfo/python-list

Re: NB question on global/local variables in functions

2006-07-15 Thread skip
Wolfgang> thanks for the hint! But what is the difference between Wolfgang> from module import * Wolfgang> and Wolfgang> import module Wolfgang> ? Try it and see. I happen to have a.py laying about: import atexit def work(): print "whew! work is hard" d

Re: NB question on global/local variables in functions

2006-07-15 Thread Wolfgang
Wolfgang schrieb: >> First, avoid "from function import *" as it pollutes your namespace. Either >> import specific symbols or just the module: >> >> from function import fun, fun1 >> import function > > thanks for the hint! But what is the difference between > from module import * >

Re: NB question on global/local variables in functions

2006-07-15 Thread Wolfgang
> > First, avoid "from function import *" as it pollutes your namespace. Either > import specific symbols or just the module: > > from function import fun, fun1 > import function thanks for the hint! But what is the difference between from module import * and import module ? >

Re: NB question on global/local variables in functions

2006-07-15 Thread skip
Wolfgang> So I have some variables which should be accessible by all my Wolfgang> functions but not accessible by the rest of my code. How can I Wolfgang> do this? Wolfgang> ###function.py: Wolfgang> c1=123.0 Wolfgang> c2=134.0 Wolfgang> def fun(temp): Wolfgang>

NB question on global/local variables in functions

2006-07-15 Thread Wolfgang
Hi all, I've started to write some functions but I have some problems with common variables in that functions. So I have some variables which should be accessible by all my functions but not accessible by the rest of my code. How can I do this? Thanks Wolfgang ###function.py: c1=123.0 c2=134