Python 2.6 Global Variables

Dave Angel davea at ieee.org
Thu Oct 29 18:52:51 EDT 2009


AK Eric wrote:
>> 2/ in Python, "global" really means "module-level" - there's nothing
>> like a "true" global namespace.
>>     
>
> Isn't that __main__?
>
>
> import __main__
> __main__.foo = "asdfasdf"
>
> print foo
> # asdfasdf
>
> Not advocating, but it does serve the purpose.
>
>   
Good that you're not advocating it, because IMHO it's bad practice to 
have circular import dependencies.  By using the __main__ alias, you 
avoid the worst problems, but that just means the others are more subtle.

You can make it safe, by carefully avoiding certain constructs.  But 
it's still fragile, in that a minor change in one module of the loop can 
cause either an exception or unexpected behavior.

DaveA



More information about the Python-list mailing list