469,271 Members | 1,484 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

global variables ?

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

ruari
Jul 18 '05 #1
5 15883
ruari mactaggart wrote:
how do I assign a value to a variable inside a function then use it in the
main body ?
If the variable name is for example 'isbadforyou', start the function with
the statement:

global isbadforyou

i am trying to unpickle a dictionary in a function to use it in the
program


By far the best approach for this is to have the function work with
local variables (faster as well as cleaner), preparing the dictionary
say in local variable 'result', and end the function with:

return result

then, the function's caller gets to decide the name (a sounder
organization!) just by calling the function in some way such as:

thenameilike = thefunction(its, arguments, ifany)
Alex

Jul 18 '05 #2

"ruari mactaggart" <ru***@charliefortune.com> wrote in message
news:bo**********@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

Jul 18 '05 #3
thank you ! It works now. This is very satisfying.

John Roth <ne********@jhrothjr.com> wrote in message
news:vq************@news.supernews.com...

"ruari mactaggart" <ru***@charliefortune.com> wrote in message
news:bo**********@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


Jul 18 '05 #4
Ulrich Petri:

Well - no. It's not.
Globals are bad, ugly, ...


Well, not in and of themselves. They're used in about 200 places in the
standard distribution, and there's talk (although no plans) of extending the
syntax further to allow designation of an encompassing (or possible other)
namespace, as in:

global var in namespace
Emile van Sebille
em***@fenx.com
Jul 18 '05 #5

"ruari mactaggart" <ru***@charliefortune.com> schrieb im Newsbeitrag
news:bo**********@news6.svr.pol.co.uk...
John Roth <ne********@jhrothjr.com> wrote in message
news:vq************@news.supernews.com...
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


thank you ! It works now. This is very satisfying.


Well - no. It's not.
Globals are bad, ugly, ...
Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Matt | last post: by
4 posts views Thread by Andrew V. Romero | last post: by
12 posts views Thread by David WOO | last post: by
2 posts views Thread by Bryan Parkoff | last post: by
5 posts views Thread by Sandman | last post: by
112 posts views Thread by istillshine | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.