[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Declaring variables in function as if at code's line-level



Brian/Matthew wrote:

> I rarely get good answers on these mailing lists. I guess I made a
> mistake asking here.

Trying. So. Hard. Not. To. Posit. A. Theory. As. To. Why...

Argh! It burns!

I can't believe I'm going to help after all that attitude, but I was a programming novice once too, although a much less belligerent one... But anyway you could put all your variables in a single global dictionary. Something like:

mydict = {}
mydict["name_spaces"] = "awesome"

def myfunction():
    mydict["but_can_be"] = "confusing to novices"

myfunction()

print mydict

In the above example, I've defined the variable "name_spaces" and "but_can_be". Note that I don't really need the underscores, spaces would work too, which is an added benefit of the dictionary approach. Also note that I didn't need to redeclare it as global, which is handy.

[Presses send and braces for a tirade about how Python dictionaries are somehow ill conceived simply because the OP doesn't understand them...]