newbee: python link objects

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jan 2007
Posts: 58
Reputation: jobs is an unknown quantity at this point 
Solved Threads: 0
jobs jobs is offline Offline
Junior Poster in Training

newbee: python link objects

 
0
  #1
Oct 15th, 2007
Why can't I have two different link objects? I create _link and _link2 as empty objects.

When I do this:
_link2 = _link,

it just creates alias _link2 to link object. Same object have 2 aliases _link2 and _link. Is there a way to copy the content of _link to _link2?

When I do this: _link2 = _link2.append("adding stuff")
why does _link2 become "None"

My code:

_link = []
site_url = "http://www.mysite.com/"
_link.append(site_url)
_link2 = []
_link2 = _link
_link2 = _link2.append("adding stuff")

print _link
print _link2

Output:

['http://www.mysite.com/', 'adding stuff']
None
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: newbee: python link objects

 
0
  #2
Oct 15th, 2007
The problem is that Python does assignment by reference. So the line

_link2 = _link

operates by making _link2 and _link both point to the same object. Change one, and you've changed them both.

If you want to copy the contents, do a slice:

_link2 = _link[:]

The operation on the left returns an entire copy of _link, which then gets assigned to _link2.

Jeff
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,028
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 932
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: newbee: python link objects

 
0
  #3
Oct 16th, 2007
Be careful with a slice copy ...
  1. old_list = [1, [2, 3,[4]], 5]
  2. new_list = old_list[:]
  3.  
  4. # now change old_list
  5. old_list[1][0] = 1111
  6.  
  7. print old_list # [1, [1111, 3, [4]], 5]
  8. print new_list # [1, [1111, 3, [4]], 5] oops!
If you want to make a copy of a list, even a deeply nested list, you best use the module copy ...
  1. import copy
  2.  
  3. old_list = [1, [2, 3,[4]], 5]
  4. new_list = copy.deepcopy(old_list)
  5.  
  6. # now change old_list
  7. old_list[1][0] = 1111
  8.  
  9. print old_list # [1, [1111, 3, [4]], 5]
  10. print new_list # [1, [2, 3, [4]], 5] now it works
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: newbee: python link objects

 
0
  #4
Oct 16th, 2007
That's awkward...
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC