how do i make an array global

Georg Brandl g.brandl-nospam at gmx.net
Wed Jun 28 15:40:01 EDT 2006


Bruno Desthuilliers wrote:
> Georg Brandl wrote:
>> 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.
> 
> Did I said so ? I just answered the OP's question. If that's the 'int'
> that confuse you, then s/int/dict/ - what I meant is that the global
> statement doesn't care about types...

Ok, I misunderstood you.

>>>>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?
> 
> Going to teach me Python, Georg ?-) Then let's be accurate first, and
> s/variable you set/name you bind/

Well, thanks. As if that made a difference.

>>  Globals are *not* evil.
> 
> Yes they are.

Really? May I tell you that in the stdlib, there are at least 13526 globals
overall?

>>>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.
> 
> The use case here is to avoid either passing a list as param or building
> and returning it - and the OP is obviously a newbie, so better for him
> to learn the RightThing(tm) from the beginning IMHO.

There's no reason to not use a global if it's the easiest thing to do.

>> For small scripts, sometimes re-assigning global names or mutating objects
>> refered to by global names is essential.
> 
> s/is essential/seems easier/
> 
> Then you or anyone else has to make a quick fix or update, and
> everything starts to break down. Too bad.

Everything starts to break down? If the change was buggy, it'll be debugged
and corrected. That's nothing particularly related to globals. Remember,
we're talking about one-file scripts here.

Georg



More information about the Python-list mailing list