global variables ?
John Roth
newsgroups at jhrothjr.com
Sat Nov 1 07:06:58 EST 2003
"ruari mactaggart" <ruari at charliefortune.com> wrote in message
news:bo04ou$382$1 at news8.svr.pol.co.uk...
> how do I assign a value to a variable inside a function then use it in the
> main body ?
>
> i am trying to unpickle a dictionary in a function to use it in the
program
Since Alex has already mentioned the 'global' statement,
I won't dwell on it. I'd just mention that it's a whole lot
simpler to use a global dictionary or an object instance
than to use a simple variable that has to be rebound.
For example:
foobar = "huh?"
def snafu():
global foobar
foobar = "what?"
Using a dictionary, it looks like this:
foodict = {foobar: "huh?"}
def snafu():
foodict["foobar"] = "what?"
That lets you consolidate all of those messy global variables
in one place as well as giving you a name you can use for
better internal documentation.
John Roth
>
> ruari
>
>
More information about the Python-list
mailing list