how do i make an array global

A

a

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

how do i declare count to be global if it is an array

subsequently i should access or define count as an array

error:
global name 'count' is not defined

thanks
-a
 
E

Erik Max Francis

a said:
def fn():
for i in range(l)
global count
count= ....

how do i declare count to be global if it is an array


count = [...]

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

Bruno Desthuilliers

a said:
def fn():
for i in range(l)

l is not defined - you should have an error here.
global count
count= ....

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)
 
G

Georg Brandl

Erik said:
a said:
def fn():
for i in range(l)
global count
count= ....

how do i declare count to be global if it is an array


count = [...]

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


No need for "global" here.

Georg
 
G

Georg Brandl

Bruno said:
a said:
def fn():
for i in range(l)

l is not defined - you should have an error here.
global count
count= ....

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.
You need to define count before.


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
 
B

Bruno Desthuilliers

Georg said:
Bruno said:
a said:
def fn():
for i in range(l)

l is not defined - you should have an error here.

global count
count= ....

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...
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.
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.
 
G

Georg Brandl

Bruno said:
Georg said:
Bruno said:
a wrote:

def fn():
for i in range(l)

l is not defined - you should have an error here.


global count
count= ....

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.
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.
Yes they are.

Really? May I tell you that in the stdlib, there are at least 13526 globals
overall?
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.
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
 
E

Erik Max Francis

Georg said:
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.
 
A

a

hi bruno georg and erik
you are right i m a newbie,
i just want to do some stuff read out some stuff from a feed and
display it in a page, so my py code, builds this list
and when it goes to cheetah it gives me an error and hence i posted
global has fixed the error but
i keep getting this again and again and i dont know why? but if i
refresh the page error goes away and the correct content comes up
please help me out


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['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?

Solution: Remove all .pyc files from you Zope tree and try again:
find <zopedir> -name "*.pyc" | xargs rm

How can we do this when code is running?

----------------------------------------------------------------

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:\usr\code.py", line 64, in GET
l_code.append( len(d_list_code['entries']) )
IndexError: list index out of range
 
A

a

please tell me how to do it if i should not make it global
like you guys worry i m not comfortable making it global and mutating
it in every subblock

thanks for reading and helping out
 
M

Marc 'BlackJack' Rintsch

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['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
 
A

a

i understand index error
but there is no index error in my code that is the problem!
Marc said:
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['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
 
F

Fredrik Lundh

a said:
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['entries']) )
> > IndexError: list index out of range


maybe you should talk to Mark ?

</F>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,132
Latest member
TeresaWcq1
Top