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