944,156 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 1663
  • Python RSS
Oct 15th, 2007
0

newbee: python link objects

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
jobs is offline Offline
58 posts
since Jan 2007
Oct 15th, 2007
0

Re: newbee: python link objects

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
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006
Oct 16th, 2007
0

Re: newbee: python link objects

Be careful with a slice copy ...
python Syntax (Toggle Plain Text)
  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 ...
python Syntax (Toggle Plain Text)
  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
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Oct 16th, 2007
0

Re: newbee: python link objects

That's awkward...
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: wxPython DateTime problems
Next Thread in Python Forum Timeline: just a question..





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC