how do i make an array global
Bruno Desthuilliers
onurb at xiludom.gro
Wed Jun 28 11:53:28 EDT 2006
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...
>
>>>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/
> Globals are *not* evil.
Yes they are.
>>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.
> 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.
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"
More information about the Python-list
mailing list