global variables ?

A

Alex Martelli

ruari said:
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
 
J

John Roth

ruari mactaggart said:
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
 
R

ruari mactaggart

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
 
R

ruari mactaggart

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

John Roth said:
ruari mactaggart said:
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
 
U

Ulrich Petri

ruari mactaggart said:
John Roth said:
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, ...
 
E

Emile van Sebille

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
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,460
Latest member
eibafima

Latest Threads

Top