globals accros modules

Stef Mientki S.Mientki-nospam at mailbox.kun.nl
Thu Jan 11 17:21:46 EST 2007


Bruno Desthuilliers wrote:
> 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
And you think physicians will believe that ?
And suppose they believe it, are the willing to stop their research to 
rethink and rewrite their code ;-)

> 
> FWIW, I'm currently fixing a simple Delphi program that's using quite a 
> few globals,
Then it was certainly not written by a Delphi-guy ;-)

  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
thanks for the tip.
> 
> # 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...
Then I miss something:

TEN = 10
TWELVE = 12
def some_function():
   global TEN
   TEN = 9
   TWELVE = 11
   print TEN, TWELVE

some_function()      #will print 9,11
print TEN, TWELVE    #will print 9,12

Or am I mistaken ?

cheers,
Stef Mientki



More information about the Python-list mailing list