Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: how do i make an array global

1 view
Skip to first unread message
Message has been deleted

Erik Max Francis

unread,
Jun 28, 2006, 1:32:57 AM6/28/06
to
a wrote:

> def fn():
> for i in range(l)
> global count
> count[i]= ....
>
> how do i declare count to be global if it is an array

count = [...]

def fn():
global count
for i in range(l):
count[i] = ...

--
Erik Max Francis && m...@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
Every human being is a problem in search of a solution.
-- Ashley Montagu

Bruno Desthuilliers

unread,
Jun 28, 2006, 6:32:19 AM6/28/06
to
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

> 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*. And
functions modifying globals is the worst possible thing. There are very
few chances you *need* a global here.

Also, and FWIW:
- Python has lists, not arrays (there's an array type in some numerical
package, but that's another beast)
- 'l' is a very bad name
- 'count' is a bad name for a list - 'counts' would be better (when I
see the name 'count', I think of an integer)

--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'on...@xiludom.gro'.split('@')])"

Georg Brandl

unread,
Jun 28, 2006, 9:52:13 AM6/28/06
to
Erik Max Francis wrote:
> a wrote:
>
>> def fn():
>> for i in range(l)
>> global count
>> count[i]= ....
>>
>> how do i declare count to be global if it is an array
>
> count = [...]
>
> def fn():
> global count
> for i in range(l):
> count[i] = ...
>

No need for "global" here.

Georg

Georg Brandl

unread,
Jun 28, 2006, 9:55:38 AM6/28/06
to
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

Bruno Desthuilliers

unread,
Jun 28, 2006, 11:53:28 AM6/28/06
to
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.

Georg Brandl

unread,
Jun 28, 2006, 3:40:01 PM6/28/06
to
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

Erik Max Francis

unread,
Jun 28, 2006, 6:10:05 PM6/28/06
to
Georg Brandl wrote:

> No need for "global" here.

Yes, that's true. I was just following the original poster's lead, but
I tend to use a `global` statement whenever I'm mutating a global in a
local block. That works as self-documentation and means you don't have
to be concerned about the precise case in which it's required, reducing
bugs when you change a block so that it would have been required if you
hadn't included it.

--
Erik Max Francis && m...@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis

Seriousness is the only refuge of the shallow.
-- Oscar Wilde

Message has been deleted
Message has been deleted

Marc 'BlackJack' Rintsch

unread,
Jun 29, 2006, 3:13:58 AM6/29/06
to
In <1151553485.3...@p79g2000cwp.googlegroups.com>, a wrote:

> Traceback (most recent call last):
> File "C:\Python24\lib\site-packages\web.py", line 2054, in
> run_wsgi_app
> result = self.server.app(env, self.wsgi_start_response)
> File "C:\Python24\lib\site-packages\web.py", line 1894, in wsgifunc
> result = func()
> File "C:\Python24\lib\site-packages\web.py", line 1872, in <lambda>
> func = lambda: handle(getattr(mod, name), mod)
> File "C:\Python24\lib\site-packages\web.py", line 1051, in handle
> return tocall(*([urllib.unquote(x) for x in args] + fna))
> File "c:\mark\web1\code.py", line 64, in GET
> l_code.append( len(d_list_code[i]['entries']) )
> IndexError: list index out of range
>
> it goes off when page is refreshed
>
> I m getting the following error, infrequently and if I refresh the
> page, it just displays the page properly
> I am unable to find the problem. How is this out of range and what
> does the error message mean?

The error message means that you try to access a list item that does not
exist::

In [1]: a = [1, 2, 3]

In [2]: a[0]
Out[2]: 1

In [3]: a[1]
Out[3]: 2

In [4]: a[2]
Out[4]: 3

In [5]: a[3]
-------------------------------------------------------------------------
exceptions.IndexError Traceback (most recent call last)

/home/marc/<ipython console>

IndexError: list index out of range

So from the traceback it seems that `i` has a value that's not between 0
and ``len(d_list_code)``.

Ciao,
Marc 'BlackJack' Rintsch

Message has been deleted

Fredrik Lundh

unread,
Jun 29, 2006, 1:18:43 PM6/29/06
to pytho...@python.org
a wrote:

> i understand index error
> but there is no index error in my code that is the problem!

so who wrote the "c:\mark\web1\code.py" file ?

> > File "c:\mark\web1\code.py", line 64, in GET
> > l_code.append( len(d_list_code[i]['entries']) )
> > IndexError: list index out of range

maybe you should talk to Mark ?

</F>

0 new messages