global variables ?

Alex Martelli aleax at aleax.it
Sat Nov 1 06:24:22 EST 2003


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





More information about the Python-list mailing list