global/local variables

Tim Rau bladedpenguin at gmail.com
Fri Jan 25 06:28:05 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.
>

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?

> > 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.
>
> > 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.
>
> Hey, it's your time and effort. If you want to, you can download the
> Python "goto" module, and use goto and comefrom in your code.
>
> No, that's unfair. Globals aren't as harmful as goto. I'm not saying that
> globals are verboten or that they have no place in 21st century
> programming at all. Since I know nothing about your program, I can't
> judge whether globals are the right way to go or not. But I am saying
> that as a general rule, reliance on global variables often leads to
> fragile code with hard-to-diagnose bugs. Your mileage may vary.
>
> --
> Steven

Ha! Gotos I understand the evil of. Human beings don't think that way.
Humans are not put together in terms of gotos. Human language does not
normally contain gotos. Global, however, seem useful to me. It seems
like there are a few things which EVERYONE should know. When you find
something often used from a variety of different places, everyone
should know about it. It saves time from developing lengthy argument
passing chains to follow the passing chains of your program. It keeps
the number of arguments low, and easily remembered. This may be more
important in python, because python has no type, and where you would
have had a type error for forgetting which arg goes where, you have
some random thing.

Also, My programming style tends towards a mongrel of functional and
object oriented programming. I tend to use lone functions when they
have no internal state, and objects when they have internal state, but
it's not ironclad. It's just what makes sense to me at the time. I'm
not defending myself, I'm just setting out my point of view. I'd like
to see what you think of it.



More information about the Python-list mailing list