global/local variables

Tim Rau bladedpenguin at gmail.com
Fri Jan 25 06:37:39 EST 2008


On Jan 25, 5:31 am, Steven D'Aprano <st... at REMOVE-THIS-
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. So when you have a line like this:
>
> nextID += 1  # equivalent to nextID = nextID + 1
>
> you are getting the value of the _local_ nextID before you have assigned
> a value to it.
>
> > I want to know why it says 'local variable' it never had a problem when
> > just allThings was in there.
>
> Because you don't assign to allThings, and therefore it is treated as
> global.
>
> > as for the objection to global variable: If I didn't make them global,
> > I'd make them part of a singleton class called gameVars that would get
> > passed everywhere.
>
> *shrug* Or you could consider a different program structure completely.
>

Oh, wait nevermind, I now understand what global nextID is for

**ENLIGHTENMENT**

      \  /\   |
    :o---  X  |
      /  \/   |

[snip]



More information about the Python-list mailing list