473,508 Members | 2,136 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

global lists

Hi everbybody again,
I have a little "problem", I don't understand the reason of this:

a = [10,1,2,3]
def foo():
global a
for el in a:
el = el*2

This doesn't make any difference, if I do
def foo():
global a
a[0] = 4

But

def foo():
global a
for n in range(len(a)):
a[n] = a[n]*2

Doesn't work either...
So I think that's pretty weird, why this happen?
I have to create a new list and assign it back to get it working??
Thaks
Jul 19 '05 #1
1 1441
On 2005-05-08, andrea crotti wrote:
I have a little "problem", I don't understand the reason of this: a = [10,1,2,3]
def foo():
global a
for el in a:
el = el*2
Simple data types (as integer) are _not_ implemented as references as
you obviously expected. Instead el is copied by value from each a[n].
Thus you only changed temporary copies.

The whole list instead _is_ a reference. If you write

b = a
b[0] = 57

a[0] will be effected too.

This doesn't make any difference, if I do
def foo():
global a
a[0] = 4
That should alter a[0].
But

def foo():
global a
for n in range(len(a)):
a[n] = a[n]*2

Doesn't work either...


It does. Test it again.

Bernd

--
Those who desire to give up freedom in order to gain security,
will not have, nor do they deserve, either one. [T. Jefferson]
Jul 19 '05 #2

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

Similar topics

88
5054
by: Tim Tyler | last post by:
PHP puts most of its functions into a big flat global namespace. That leads to short function names - but creates a namespace minefield for programmers. Lots of the functions are legacies from...
10
17831
by: Matt | last post by:
Greetings, What are people's thoughts on global variables in C++? Why are we taught not to use them in programming? Is it true that if you are running two copies of the C program one copy can...
2
2216
by: Tim | last post by:
Please advise if you can. Presumably initialisation of members in member initialisation lists is perfomed by 'C' run-time startup. If the CRT was never started-up would those members be garbage?...
59
3874
by: seberino | last post by:
I've heard 2 people complain that word 'global' is confusing. Perhaps 'modulescope' or 'module' would be better? Am I the first peope to have thought of this and suggested it? Is this a...
18
8621
by: vib | last post by:
Hi there, By chance, I came to learn that it is bad programming practice to initialize global variables at outside of programs. Is it that bad? In order to fullfil this, I had to declare them...
2
5842
by: a | last post by:
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:
9
18853
by: k04jg02 | last post by:
I want to make a Python app that runs in the background, and when a user hits a key combination, for a function to run. This sounds simple enough, but all of the keypress detecting libraries I can...
3
2744
by: dn6326 | last post by:
hi, essentially, my program needs to store an array of lists so that in main() it can have 2 run modes from the command line. i.e. i can run the program with ./matrix load filename to load a...
4
10251
by: Joe | last post by:
Hello all! I added a Global.asax to my application. I'm using the Application_BeginRequest event. Everything works fine in my development enviorment but when I publish the web site the...
2
1640
by: RgeeK | last post by:
I'm seeing something which make me think I'm missing something about how global var's behave. I've defined a global string, right at the start of my .py file. outXMLfile = "abc" I define a...
0
7225
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,...
0
7326
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7046
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...
0
5629
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,...
1
5053
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4707
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...
0
3182
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
418
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...

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.