Python 2.6 Global Variables

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Thu Oct 29 05:35:27 EDT 2009


Ronn Ross a écrit :
(please don't top-post - fixed)
> On Oct 28, 2009, at 20:50, mattofak <mattofak at gmail.com> wrote:
> 
>> Hi All;
>>
>> I'm new to Python and moving from C, which is probably a big source of
>> my confusion. I'm struggling with something right now though and I
>> hope you all can help.
>>
>> I have a global configuration that I would like all my classes and
>> modules to be able to access. What is the correct way to do this?
>>

 > Inside the method that you want to use  the var prefix the first
 > instance with global. For example: global my_var. Then you can use the
 > var like normal in the method. Good luck


Wrong, and wrong again.

1/ you don't have to use the "global" statement to access a "global" 
name - only if you plan on rebinding it (which is more often than not a 
bad idea but that's another problem)

2/ in Python, "global" really means "module-level" - there's nothing 
like a "true" global namespace.



More information about the Python-list mailing list