global name is not defined - error - but for array

Fredrik Lundh fredrik at
Wed Jun 28 09:43:51 CEST 2006

"a" <a at> 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

a couple of notes:

1) global statements should be placed at the top of the function

2) objects don't appear out of nowhere; you need to initialize the "count"
list before you can assign stuff to it.  this is what causes the exception; the
global statement has nothing to do with that.

3) it's called "list" on Python, not "array".

4) if you read the tutorial, you'll learn better ways to loop over lists, and
build new lists.  see the chapters "An Informal Introduction to Python",
"More Control Flow Tools", and "Data Structures".

5) posting one copy of your question is enough; if you feel that you want
to try out different subject lines, do that for yourself, before posting your
message to the list.

6) this list isn't hosted by googlegroups, so cancelling a post in googlegroups
doesn't mean that other people won't see it.  there's a world outside google
too, you know...


More information about the Python-list mailing list