473,327 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

possible python bug here

In the sample below, the foo function modify the b list,
but I think it should modify only c, not b! It work
correctly if if b is one dimension list instead two.

def foo(aList):
print "use foo..."
aList[2][0] += .35
aList[2][1] += .35
aList[2][2] += .35

p = [2.5,2.5,2.5]
b = [p,p,p,p]

c=b[:] #c is a totally new list cloned from b, it's not an alias!

print "b = ",b[2]

foo(c) #The argument is c, not b! Why b is modified too?

print "b = ", b[2]# after foo(c), the list b now is different!
Jul 18 '05 #1
3 1402
manuel schreef:
In the sample below, the foo function modify the b list,
but I think it should modify only c, not b! It work
correctly if if b is one dimension list instead two.

def foo(aList):
print "use foo..."
aList[2][0] += .35
aList[2][1] += .35
aList[2][2] += .35

p = [2.5,2.5,2.5]
b = [p,p,p,p]

c=b[:] #c is a totally new list cloned from b, it's not an alias!


No it isn't. You didn't make a deep copy.

--
Peter Kleiweg L:NL,af,da,de,en,ia,nds,no,sv,(fr,it) S:NL,de,en,(da,ia)
info: http://www.let.rug.nl/~kleiweg/ls.html

The Halloween Documents: http://www.opensource.org/halloween/

Jul 18 '05 #2
Peter Kleiweg wrote:
manuel schreef:
c=b[:] #c is a totally new list cloned from b, it's not an alias!

No it isn't. You didn't make a deep copy.


Oh..thank you very much!
Jul 18 '05 #3
manuel wrote:
In the sample below, the foo function modify the b list,
but I think it should modify only c, not b! It work
correctly if if b is one dimension list instead two.

def foo(aList):
print "use foo..."
aList[2][0] += .35
aList[2][1] += .35
aList[2][2] += .35

p = [2.5,2.5,2.5] Here you bind the name p to a list. b = [p,p,p,p] Here you bind the name b to a list, which has four references to list p
c=b[:] #c is a totally new list cloned from b, it's not an alias! Here you bind the name c to a list, which has four copies of the
references to list p
print "b = ",b[2] Here you print the third reference to p of list b
foo(c) #The argument is c, not b! Why b is modified too? Here you modify the third reference to p of list c
print "b = ", b[2]# after foo(c), the list b now is different!

Here you print the third reference to p of list b, it's still just
reference to list (originally bound to) p.

Note that also the other references to the changed list (b[0:4], c[0:4],
p) refer to the same changed list.
Jul 18 '05 #4

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

Similar topics

5
by: MiLF | last post by:
Is it possible to write a Audio CD Player by using python only?
5
by: AMD | last post by:
Hi, I need to write a Win32 DLL and I would like to use Python instead of VB, C++ or Delphi. Is this possible? Thank you, Andre M. Descombes
8
by: Tim Tyler | last post by:
Like C, Python seems to insist I declare functions before calling them - rather than, say, scanning to the end of the current script when it can't immediately find what function I'm referring to. ...
40
by: Ron Adam | last post by:
After considering several alternatives and trying out a few ideas with a modified list object Bengt Richter posted, (Thank You), I think I've found a way to make slice operation (especially far end...
1
by: Joe Peterson | last post by:
I've been doing a lot of searching on the topic of one of Python's more disturbing issues (at least to me): the fact that if a __del__ finalizer is defined and a cyclic (circular) reference is...
9
by: manstey | last post by:
Hi, My question probably reflects my misunderstanding of python objects, but I would still like to know the answer. The question is, is it possible for an instnace to have a value (say a...
25
by: jwrweatherley | last post by:
I'm pretty new to python, but am very happy with it. As well as using it at work I've been using it to solve various puzzles on the Project Euler site - http://projecteuler.net. So far it has not...
71
by: Jack | last post by:
I understand that the standard Python distribution is considered the C-Python. Howerver, the current C-Python is really a combination of C and Python implementation. There are about 2000 Python...
9
by: igor.tatarinov | last post by:
Hi, I am pretty new to Python and trying to use it for a relatively simple problem of loading a 5 million line text file and converting it into a few binary files. The text file has a fixed format...
3
by: Eric Brunel | last post by:
Hello all, I'm trying out Python 2.6 and I found what might be a bug in the Tkinter module. How can I report it? The possible bug is a traceback when trying to delete a menu item in a menu...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.