473,761 Members | 10,498 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how do i make an array global

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

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

Jun 28 '06 #1
12 2230
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 && ma*@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
Jun 28 '06 #2
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 'o****@xiludom. gro'.split('@')])"
Jun 28 '06 #3
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
Jun 28 '06 #4
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
Jun 28 '06 #5
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...
subsequent ly 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 'o****@xiludom. gro'.split('@')])"
Jun 28 '06 #6
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.
subsequentl y 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
Jun 28 '06 #7
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 && ma*@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
Jun 28 '06 #8
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\li b\site-packages\web.py ", line 2054, in
run_wsgi_app
result = self.server.app (env, self.wsgi_start _response)
File "C:\Python24\li b\site-packages\web.py ", line 1894, in wsgifunc
result = func()
File "C:\Python24\li b\site-packages\web.py ", line 1872, in <lambda>
func = lambda: handle(getattr( mod, name), mod)
File "C:\Python24\li b\site-packages\web.py ", line 1051, in handle
return tocall(*([urllib.unquote( x) for x in args] + fna))
File "c:\mark\web1\c ode.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?

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\li b\site-packages\web.py ", line 2054, in
run_wsgi_app
result = self.server.app (env, self.wsgi_start _response)
File "C:\Python24\li b\site-packages\web.py ", line 1894, in wsgifunc
result = func()
File "C:\Python24\li b\site-packages\web.py ", line 1872, in <lambda>
func = lambda: handle(getattr( mod, name), mod)
File "C:\Python24\li b\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[i]['entries']) )
IndexError: list index out of range
Erik Max Francis wrote:
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 && ma*@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


Jun 29 '06 #9
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

Jun 29 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
2346
by: Bob | last post by:
I've tried everything; and I can't seem to get past this VERY (seemingly) simply problem. I want to work with an array variable within a function(s). I can't get it to work; if I: 1) global $arr=array(); (syntax err) 2) global $arr; in "main", the var isn't global 3) global $arr; in function, the array is cleared each time
6
2371
by: billy | last post by:
I've got a set of subclasses that each derive from a common base class. What I'd like to do is create a global array of the class types (or, class names) that a manager class can walk through in its constructor and instantiate one of each of the class types in this global array. So, it's almost like the global array has to hold data types as opposed to data. Basically, I currently have to add the items manually 1 at a time.
9
10706
by: matthurne | last post by:
I need to send just an array to a function which then needs to go through each element in the array. I tried using sizeof(array) / sizeof(array) but since the array is passed into the function, sizeof(array) is really sizeof(pointer to first element in array) and therefore doesn't solve my problem. If I can't calculate the size of the array, can I go through the elements without knowing the size and somehow test whether I'm off the end...
7
2456
by: Ekim | last post by:
hello, I'm using MANAGED C++ and need one of the following two questions answered: 1.) How can one make a global managed array? I'm thinking on something like defining static System::Byte globalArray __gc; at the begin of a file. But I get the error "global Array: cannot declare a global or static managed type object or a __gc pointer"
7
2159
by: pauld | last post by:
Ive got a series of 2 dimensional associative arrays while(list($key, $val) = each ($results)) { {foreach($val as $i=>$v) print "$i = $v"; } } but i want to define the order that the various $i's are printed in
2
3583
by: pengbsam | last post by:
Hello All: Having a problem with arraylist.copyto function. And here's a sample of my code: In global--> public struct point { int x; string y; } static public point point;
4
2811
by: james_027 | last post by:
hi, There is something that I don't understand well, I use array for combining strings like items = .join(''); in FF this works well but in IE I had to add something like this var items = new Array();
5
3651
by: Immortal Nephi | last post by:
I would like to design an object using class. How can this class contain 10 member functions. Put 10 member functions into member function pointer array. One member function uses switch to call 10 member functions. Can switch be replaced to member function pointer array? Please provide me an example of source code to show smart pointer inside class. Thanks....
6
1818
by: remlostime | last post by:
now, i write some code code1: int a; int main(){} code2: vector<inta(10000000); int main(){} after using g++ compile, and run it, code1 is broken, but code2 runs
0
9521
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9333
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9900
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8768
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6599
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5361
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3863
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3442
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2733
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.