how do i make an array global

Georg Brandl g.brandl-nospam at gmx.net
Wed Jun 28 09:55:38 EDT 2006


Bruno Desthuilliers wrote:
> a wrote:
>> def fn():
>>      for i in range(l)
> 
> l is not defined - you should have an error here.
> 
>>            global count
>>            count[i]= ....
>> 
>> how do i declare count to be global if it is an array
>
> Just like it was an integer

No. If he's only mutating "count", he doesn't need a global
declaration.

>> subsequently i should access or define count as an array
> 
> You need to define count before.
> 
>> error:
>> global name 'count' is not defined
> 
> He...
> 
> *but*
> You probably should not do that anyway. Globals are *evil*.

Do you realize that every variable you set in a module's namespace is a
global when used by a function? Globals are *not* evil.

> And functions modifying globals is the worst possible thing.
> There are very few chances you *need* a global here.

Look at the use case first.
For small scripts, sometimes re-assigning global names or mutating objects
refered to by global names is essential. For large-scale packages, though,
I agree with you that mutating globals is bad.

Georg



More information about the Python-list mailing list