global and None

Sean 'Shaleh' Perry shaleh at speakeasy.net
Tue Dec 23 04:12:56 EST 2003


On Monday 22 December 2003 23:17, Leo Yee wrote:
>
> So I try to solve the problem in this way:
> --- code begin ---
>
> global g
> g=None
> def foo():
>     global g
>     if g==None:
>         g=0
> foo()
>
> --- code end ---
>
> And it works but the python documentation told me the following:
> "Names listed in a global statement must not be defined as formal
> parameters or in a for loop control target, class definition, function
> definition, or import statement. "
>
> Does anyone know what I should do to solve this problem? Thanks in advance.

It is warning you against shadowing.  For instance:

g = None

def foo():
  global g
  for g in some_thing:
    act(g)

Your code is correct.  Of course, you have to ask yourself if a global is 
really the right choice.






More information about the Python-list mailing list