create global variables?

Jason tenax.raccoon at gmail.com
Mon Oct 30 17:13:54 EST 2006


Alistair King wrote:
> have tried:
>
> def monoVarcalc(atom):
>     a = atom + 'aa'
>     Xaa = a.strip('\'')
>     m = atom + 'ma'
>     Xma = m.strip('\'')
>     global Xma
>     global Xaa
>     Xaa = DS1v.get(atom)
>     Xma = pt.get(atom)
>     print Xma
>     print Xaa
>
> monoVarcalc('C')
>
> print Caa
> print Cma
> .......................................................................................................
>
> where global Xma & Xaa are before and after any further functions
>
> i get still get the error
>
> Traceback (most recent call last):
>   File "DS1excessH2O.py", line 54, in ?
>     print Caa
> NameError: name 'Caa' is not defined

Mr King, please look at the documentation for the global keyword.  You
can either see it at "http://docs.python.org/ref/global.html#l2h-564".
You can also access information about it using Python's help function
(use "help('global')" to see its help).

The global statement makes the names that follow available globally.
It does *not* evaluate the name in any way.  If you have "global Xma"
in your code as above, the name Xma becomes a global name.  That name
happens to contain the string value "Caa" in it.  The local name "Caa"
is not placed into the global name-space because you didn't put it into
the global statement.

Heck, I don't see that you have a local variable with the name "Caa".
You have three local variables and two global variables in the
monoVarcalc function:
  -- "atom" is the name of the parameter to the function.  The name is
only local to the function.
  -- "a" and "m" are local variables in the function.  They are the
result of adding strings to atom.
  -- "Xaa" and "Xma" are global names.  They are assigned values from
the local variables "m" and "a".  You then overwrite their values with
the global object DSv1's and pt's get methods.

As others have shown, you can use the dictionary-like object returned
by the globals() function to place arbitrary names into the global
namespace.

However, I highly recommend that you review your algorithms.  I
generally find that programs designed with many globals is a sign that
the program is badly designed.  I also recommend that you resolve any
syntax warnings that your program may be generating (hint: relocate
your global statement to the top of your function).

Your program could probably benefit from a number of things that Python
does well:

  -- Python has very efficient dictionaries.  Dictionaries allow you to
easily pass around things like your values in the above program.

  -- Python makes it trivial to return multiple values.  If you simply
need to return more than one value from the function, just return a
tuple.

  -- Python's object-oriented programming model and nested name-spaces
are pretty easy to use.  If you are unfamiliar with object-oriented
programming, there are a number of tutorials out there that can help.
You don't need to use objects with Python, but there are many cases
where they will "obviously" work far better than ugly procedural-level
hacks.  (By obviously, I mean that it will be pretty easy to understand
exactly what you did later on.)

Python's website has a tutorial section
(http://www.python.org/doc/Intros.html) and many people recommend the
"Dive Into Python" tutorial series (http://www.diveintopython.org/).
Do a google search for "Python tutorials" for more information.

    --Jason




More information about the Python-list mailing list