Warning when doubly linked list is defined gloablly

Peter Otten __peter__ at web.de
Mon Sep 5 09:07:52 EDT 2005


chand wrote:

> Sorry for mailing the code which does't even compile !!

You probably missed that other hint where Fredrik Lunddh told you to
somewhat reduce the script's size.

> Please find below the code which compiles. Please let me know how to
> resolve this warning message..!!
> SyntaxWarning: name 'g_opt_list' is used prior to global declaration

This warning occurs if a global variable is used before it is declared as
such:

>>> def f():
...     v = 42
...     global v
...
<stdin>:1: SyntaxWarning: name 'v' is assigned to before global declaration

This kind of code is dangerous because a casual reader might think that v is
a local variable. You can avoid the warning -- and the source of confusion
-- by moving the declaration to before the variable's first usage (I prefer
the top of the function):

>>> def f():
...     global v
...     v = 42
...

In your script things are even more messed up. You declare g_opt_list twice:

>>> def f():
...     global v
...     v = 42
...     global v
...
<stdin>:1: SyntaxWarning: name 'v' is assigned to before global declaration

Now that you understand the cause of the warning it is time to proceed to
yet another effbot hint: You never rebind the global variable g_opt_list
and therefore do not need the 

global g_opt_list

statement at all.

Peter




More information about the Python-list mailing list