globals accros modules

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Thu Jan 11 15:50:48 EST 2007


Stef Mientki a écrit :
> Bruno Desthuilliers wrote:
> 
>> stef a écrit :
(snip)
>>> You can explain your collegaes, that
>>> - the startindex of arrays changes from 1 to 0
>>> - slices are upto, instead of including the final border
>>> - indention is thé key
>>> And tell them about all beautiful things in Python,
>>> but tell them that they are going to loose all their globals ???
>>
>>
>> It's a feature. Globals are definitively a BadThing(tm).
> 
> 
> yes, I know, but my audience will accept that only in the long term.

Unless you clearly explain the benefits... Any code relying on the 
existence of a global is:
1/ dependent on the existence of this global
2/ harder to understand

FWIW, I'm currently fixing a simple Delphi program that's using quite a 
few globals, and since I'm not familiar with ObjectPascal (my experience 
with Pascal boils down to a few cs101 stuff like implementing a linked 
list, some 6 or 7 years ago), I'm losing a lost of time with these [bip] 
globals...

> But maybe this idea works:
> 
> <file Ugly_MatLab_Globals.py>
> global var1
> global var2
> </file Ugly_MatLab_Globals.py>

The 'global' statement only makes sens within a function, and it's only 
a declaration, not a definition (-> it won't bind the following name by 
itself - only tell the interpreter that this name is to be considered as 
belonging to the module's namesepace ).

The minimal working example requires that you assign a default value:

# myglobs.py
meaning_of_life = 42

# another.py
import myglobs
print myglobs.meaning_of_life

> 
> <all other py-files in the project>
> import Ugly_MatLab_Globals
> 
> def some_function():
>   import Ugly_MatLab_Globals

You don't have to reimport it here...

> 
> (btw who owns the BadThing tm ;-)

I Do(tm) !-)



More information about the Python-list mailing list