Guilty secret: globals baffle me

Martin v. Löwis loewis at informatik.hu-berlin.de
Wed Nov 6 09:54:45 EST 2002


"Edward K. Ream" <edream at tds.net> writes:

> 1. How many globals are there?  Is there just one, or does each module
> define its own?

Each module has its own set of globals. There is only one set of truly
global names, which are the builtin names (to which you can also add,
but that is considered a hack).

> only works if the two parts are within the same module, so I think maybe
> globals() returns module specific stuff, but then why call it globals?

Because it is not local :-) It's more a distinction of lifetime, then
of visibility. Local variables life for the duration of the function;
globals live across arbitrary function calls, starting with the module
import (or first assignment) until the interpreter end (or del
statement).

You can share globals across modules by means of import. If you want
to assign to the global, make sure you don't use from-import, but
import the module, then assign via module.globalname = value.

Regards,
Martin



More information about the Python-list mailing list