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
> is, it's at the top level of a module, it is global.
>
> More interesting is if it is inside a function or class. If you assign to
> the name in the function, Python assumes it is local *unless* you have
> defined it as global with the global statement.
>
> Otherwise, Python will treat it as global.
>
> > 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:
>
> I've tried to run your code -- and it isn't easy, involving much
> backwards-and-forwards copying and pasting, thanks to word-wrap in you
> post -- and the error I get is:
>
> SyntaxError: invalid syntax
>
> because you left off the colon from the first if statement.
>
> I'm not especially inclined to start hunting through your code fixing any
> other errors in order to find the error you think you're getting. How
> about if you quote the exception traceback you actually get?
>
> > I don't think it's important, but the function is defined before
> > the two globals.
>
> Nope, not important. What's important is whether the function is *called*
> before or after the globals are created.
That's been my experience so far.

>
> If you're using global variables in your code, you probably should stop.
> As a general rule, global variables are poor programming practice for a
> number of reasons. Google on "Global variables considered harmful" for
> some examples.
>
> --
> Steven

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 (most recent call last):
  File "C:\Documents and Settings\Owner\My Documents\NIm's code\sandbox
\sandbox.py", line 248, in <module>
    thing.step()
  File "C:\Documents and Settings\Owner\My Documents\NIm's code\sandbox
\sandbox.py", line 112, in step
    allThings[len(allThings)-1].id = nextID
UnboundLocalError: local variable 'nextID' referenced before
assignment

I want to know why it says 'local variable' it never had a problem
when just allThings was in there.

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. It's unlikely that they'll get mixed in with
anyhting else, as they fulfill a unique function. Also, I think it's
more convenient, and I am, after all, my own employer when it comes to
programming.
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to