global declarations
Peter Hansen
peter at engcorp.com
Sun Dec 22 22:42:40 EST 2002
Frederik Fuest wrote:
>
> i've a problem with the 'global variables' in classes.
> It seems, that it isnt possible to make a real global declaration of
> lists or strings in a class. Whatever i try, if i call functions from
> other functions in my class, these functions are no longer able to know
> the global variables!?
>
> How could i handle this?
You will need to use the global statement for those variables
in *every* function which can write to them. The "global"
statement basically tells the compiler "don't make this variable
be local to this function, but place it in the module dict
when I create it".
Also note that "global" really just means "global to this module",
as it is not possible(*) in Python to make a "real" global (i.e.
one which is global to the entire application).
(*) Naturally, this being Python, you can work around this and
actually *do* the impossible, through the __builtin__ module,
but you really don't want to do that in your application.
-Peter
More information about the Python-list
mailing list