473,787 Members | 2,931 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

scared about refrences...

I'm really worried that python may is doing some things I wasn't
expecting... but lets see...

if I pass a list to a function def fn(myList):

and in that function I modify an element in the list, then does the
callers list get modied as well.

def fn(list):
list[1] = 0

myList = [1, 2, 3]
print myList
fn(myList)
print myList
>>[1,2,3]
[1,0,3]
How can I avoid this? In this case this is a really simplified example
but the effects are the same...
How do I specify or create deep copies of objects that may contain
other objects that may contain other
object that may contain other objects....

Oct 30 '06 #1
28 1231
In <11************ **********@b28g 2000cwb.googleg roups.com>, SpreadTooThin
wrote:
I'm really worried that python may is doing some things I wasn't
expecting... but lets see...
Expect that Python never copies something if don't ask explicitly for a
copy.
if I pass a list to a function def fn(myList):

and in that function I modify an element in the list, then does the
callers list get modied as well.

def fn(list):
list[1] = 0

myList = [1, 2, 3]
print myList
fn(myList)
print myList
>>>[1,2,3]
[1,0,3]

How can I avoid this? In this case this is a really simplified example
but the effects are the same...
In this case:

def fn(lst):
lst = list(lst)
lst[1] = 0

How do I specify or create deep copies of objects that may contain
other objects that may contain other object that may contain other
objects....
See the `copy` module especially `copy.deepcopy( )`.

Ciao,
Marc 'BlackJack' Rintsch

Oct 30 '06 #2
On 2006-10-30, SpreadTooThin <bj********@gma il.comwrote:
def fn(list):
list[1] = 0

myList = [1, 2, 3]
print myList
fn(myList)
print myList
>>>[1,2,3]
[1,0,3]

How can I avoid this? In this case this is a really simplified
example but the effects are the same... How do I specify or
create deep copies of objects that may contain other objects
that may contain other object that may contain other
objects....
See 3.18 Copy -- Shallow and deep copy operations.

--
Neil Cerutti
I pulled into a lay-by with smoke coming from under the bonnet. I
realized the car was on fire so took my dog and smothered it with a
blanket. --Insurance Claim Blooper
Oct 30 '06 #3
I am no Python guru - just an ordinary user.

There is nothing "scary" about this. There are (many) situations where
this is actually *desirable* but of course there are (many) situations
where this is an unwelcomed side-effect.

In situations where I don't want this to happen, I simply pass down the
list as an non-mutable object (like converting the list to a tuple).

It took me a little bit of getting used to this concept as well:
everything is either a mutable object, or a non-mutable object. I just
have to throw away trying to use concept of "pointers" in Python.

SpreadTooThin wrote:
I'm really worried that python may is doing some things I wasn't
expecting... but lets see...

if I pass a list to a function def fn(myList):

and in that function I modify an element in the list, then does the
callers list get modied as well.

def fn(list):
list[1] = 0

myList = [1, 2, 3]
print myList
fn(myList)
print myList
>[1,2,3]
[1,0,3]

How can I avoid this? In this case this is a really simplified example
but the effects are the same...
How do I specify or create deep copies of objects that may contain
other objects that may contain other
object that may contain other objects....
Oct 30 '06 #4
SpreadTooThin wrote:
I'm really worried that python may is doing some things I wasn't
expecting... but lets see...

if I pass a list to a function def fn(myList):

and in that function I modify an element in the list, then does the
callers list get modied as well.

def fn(list):
list[1] = 0

myList = [1, 2, 3]
print myList
fn(myList)
print myList
>>>[1,2,3]
[1,0,3]

How can I avoid this?
by not modifying your arguments nilly-willy, of course.
How do I specify or create deep copies of objects that may contain
other objects that may contain other
object that may contain other objects....
http://www.effbot.org/pyfaq/how-do-i...-in-python.htm

but if you find yourself needing to copy things a lot, you need to look
over your design. well-designed Python code seldom needs to copy object
values.

</F>

Oct 30 '06 #5

Marc 'BlackJack' Rintsch wrote:
In <11************ **********@b28g 2000cwb.googleg roups.com>, SpreadTooThin
wrote:
I'm really worried that python may is doing some things I wasn't
expecting... but lets see...

Expect that Python never copies something if don't ask explicitly for a
copy.
if I pass a list to a function def fn(myList):

and in that function I modify an element in the list, then does the
callers list get modied as well.

def fn(list):
list[1] = 0

myList = [1, 2, 3]
print myList
fn(myList)
print myList
>>[1,2,3]
[1,0,3]
How can I avoid this? In this case this is a really simplified example
but the effects are the same...

In this case:

def fn(lst):
lst = list(lst)
lst[1] = 0

How do I specify or create deep copies of objects that may contain
other objects that may contain other object that may contain other
objects....

See the `copy` module especially `copy.deepcopy( )`.
I'm aware of __deepcopy__ but does that mean that every object in the
object
needs to have its own deep copy? And how do I ensure that?

Ciao,
Marc 'BlackJack' Rintsch
Oct 30 '06 #6

Marc 'BlackJack' Rintsch wrote:
In <11************ **********@b28g 2000cwb.googleg roups.com>, SpreadTooThin
wrote:
I'm really worried that python may is doing some things I wasn't
expecting... but lets see...

Expect that Python never copies something if don't ask explicitly for a
copy.
if I pass a list to a function def fn(myList):

and in that function I modify an element in the list, then does the
callers list get modied as well.

def fn(list):
list[1] = 0

myList = [1, 2, 3]
print myList
fn(myList)
print myList
>>[1,2,3]
[1,0,3]
How can I avoid this? In this case this is a really simplified example
but the effects are the same...

In this case:

def fn(lst):
lst = list(lst)
lst[1] = 0

How do I specify or create deep copies of objects that may contain
other objects that may contain other object that may contain other
objects....

See the `copy` module especially `copy.deepcopy( )`.
This appears to be the right thing to do to me.. (but what do I know?)
I tried this which more closely resembles my project but this doesn't
work:

import array
import copy

class test:
def __init__(self):
self.a = array.array('H' , [1, 2, 3])
self.b = ['a', 'b', 'c']

def dump(self):
print self.a, self.b

t = test()
t.dump()

def testit(x):
t = copy.deepcopy(x )
t.a[1] = 0
t.b[1] = 0

testit(t)

t.dump()

Oct 30 '06 #7
On Mon, 30 Oct 2006 13:10:47 -0800, SpreadTooThin wrote:
How do I specify or create deep copies of objects that may contain
other objects that may contain other object that may contain other
objects....

See the `copy` module especially `copy.deepcopy( )`.

This appears to be the right thing to do to me.. (but what do I know?)
Yes, copy.deepcopy() is the thing you want.

But remember Fredrik's advice that well-designed Python code should not
need to copy data structures often. I don't think I've ever needed to use
deepcopy, and rarely copy.copy().

In general, functions should not modify their caller's data. So this is
bad practice:

def print_list(alis t):
"""Print a sorted list"""
alist.sort() # modifies the caller's data -- bad!
for index, value in enumerate:
print "Value %s at index %d" % (index, value)

This is better:

def print_list(alis t):
"""Print a sorted list"""
alist = alist[:] # makes a local shallow copy of the list
alist.sort() # safe to modify now
for index, value in enumerate:
print "Value %s at index %d" % (index, value)

But notice that you only need a shallow copy, not a deep copy, because you
aren't modifying the objects within the list, only the list itself.
I tried this which more closely resembles my project but this doesn't
work:
Unfortunately my crystal ball is back at the shop being repaired, so
you'll have to explain what "doesn't work" means in this case. Does it
raise an exception? If so, please post the exception. Does it do something
different from what you expected? Then what did you expect, and what did
it do?
--
Steven.

Oct 30 '06 #8

Steven D'Aprano wrote:
On Mon, 30 Oct 2006 13:10:47 -0800, SpreadTooThin wrote:
How do I specify or create deep copies of objects that may contain
other objects that may contain other object that may contain other
objects....

See the `copy` module especially `copy.deepcopy( )`.
This appears to be the right thing to do to me.. (but what do I know?)

Yes, copy.deepcopy() is the thing you want.

But remember Fredrik's advice that well-designed Python code should not
need to copy data structures often. I don't think I've ever needed to use
deepcopy, and rarely copy.copy().

In general, functions should not modify their caller's data. So this is
bad practice:

def print_list(alis t):
"""Print a sorted list"""
alist.sort() # modifies the caller's data -- bad!
for index, value in enumerate:
print "Value %s at index %d" % (index, value)

This is better:

def print_list(alis t):
"""Print a sorted list"""
alist = alist[:] # makes a local shallow copy of the list
alist.sort() # safe to modify now
for index, value in enumerate:
print "Value %s at index %d" % (index, value)

But notice that you only need a shallow copy, not a deep copy, because you
aren't modifying the objects within the list, only the list itself.
I tried this which more closely resembles my project but this doesn't
work:

Unfortunately my crystal ball is back at the shop being repaired, so
you'll have to explain what "doesn't work" means in this case. Does it
raise an exception? If so, please post the exception. Does it do something
different from what you expected? Then what did you expect, and what did
it do?
I seems that some of the objects in the list don't get along well with
deep copy..
See my second example post that used deepcopy... When run blows up...


>
--
Steven.
Oct 30 '06 #9
SpreadTooThin wrote:
Steven D'Aprano wrote:
>On Mon, 30 Oct 2006 13:10:47 -0800, SpreadTooThin wrote:
>>>>How do I specify or create deep copies of objects that may contain
other objects that may contain other object that may contain other
objects.. ..
See the `copy` module especially `copy.deepcopy( )`.

This appears to be the right thing to do to me.. (but what do I know?)
Yes, copy.deepcopy() is the thing you want.

But remember Fredrik's advice that well-designed Python code should not
need to copy data structures often. I don't think I've ever needed to use
deepcopy, and rarely copy.copy().

In general, functions should not modify their caller's data. So this is
bad practice:

def print_list(alis t):
"""Print a sorted list"""
alist.sort() # modifies the caller's data -- bad!
for index, value in enumerate:
print "Value %s at index %d" % (index, value)

This is better:

def print_list(alis t):
"""Print a sorted list"""
alist = alist[:] # makes a local shallow copy of the list
alist.sort() # safe to modify now
for index, value in enumerate:
print "Value %s at index %d" % (index, value)

But notice that you only need a shallow copy, not a deep copy, because you
aren't modifying the objects within the list, only the list itself.
>>I tried this which more closely resembles my project but this doesn't
work:
Unfortunatel y my crystal ball is back at the shop being repaired, so
you'll have to explain what "doesn't work" means in this case. Does it
raise an exception? If so, please post the exception. Does it do something
different from what you expected? Then what did you expect, and what did
it do?
I seems that some of the objects in the list don't get along well with
deep copy..
See my second example post that used deepcopy... When run blows up...
When it blows up, is there a lot of shrapnel, or just smoke and fire?
Is the shrapnel mostly metal, or is it plastic and glass?

In short, if we don't know what's happening, we can't help.
* Did the program spit out a bunch of text you didn't understand?
If so, show us the text. That text may be incomprehensibl e at first,
but it contains crucial clues.

* Did it close your python window without a word?
Tell us.

* Did your computer freeze up?
Tell us.

If you don't tell us what went wrong *exactly*, you won't get a
satisfactory answer.

Cheers,
Cliff
Oct 30 '06 #10

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

Similar topics

0
1086
by: Lawrence Oluyede | last post by:
http://www.cardboard.nu/archives/000118.html I think that the guy (Alan Green) is very scared :) I read also a funny (but clever) comment to that post (the first one): " True, True. In fact, we should boycott Java, too. C is the way to go! (Or Assembler? Or just write the binary code by hand?) "
3
1373
by: Lawrence Oluyede | last post by:
http://www.cardboard.nu/archives/000118.html I think that the guy (Alan Green) is very scared :) I read also a funny (but clever) comment to that post (the first one): " True, True. In fact, we should boycott Java, too. C is the way to go! (Or Assembler? Or just write the binary code by hand?) "
0
1093
by: sonic_soul | last post by:
hi i have a solution with 22 loaded projects. 1 of those projects is a web app, and this web app is using functionality from the other 21 projects which are simple dll's in my web project i need to add a refrence to other projects so that the dll's are automatically copied to my web app/bin directory. i can do that for all but a couple of them..
0
900
by: Josh | last post by:
Hi Guys, I have lots of web refrences connecting to different sites and i need to be able to dynamically call the web refrence based on output from a database. At the moment i am doing the following; //send execution command to the client to update the database wrAmp.dbExecutor dbe = new wrAmp.dbExecutor(); bool barf = dbe.updateDatabase();
3
1569
by: Afshar | last post by:
Hi, I am writing a single .cs in IDE that uses some refrences that I have created previously as dll. I compile this refrences with /r switch in csc.exe with no problem. IntelliSense works with cs commands and syntaxes. But the problem is It doesn't know my own refrences and so doesn't show my class members. What can I do with with these? Please Help! Regards,
0
9498
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,...
0
10363
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10172
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10110
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
8993
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...
1
7517
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5535
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
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.