| | |
newbee: python link objects
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Jan 2007
Posts: 58
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Jul 2006
Posts: 608
Reputation:
Solved Threads: 150
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
_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
Be careful with a slice copy ...
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)
old_list = [1, [2, 3,[4]], 5] new_list = old_list[:] # now change old_list old_list[1][0] = 1111 print old_list # [1, [1111, 3, [4]], 5] print new_list # [1, [1111, 3, [4]], 5] oops!
python Syntax (Toggle Plain Text)
import copy old_list = [1, [2, 3,[4]], 5] new_list = copy.deepcopy(old_list) # now change old_list old_list[1][0] = 1111 print old_list # [1, [1111, 3, [4]], 5] print new_list # [1, [2, 3, [4]], 5] now it works
May 'the Google' be with you!
![]() |
Similar Threads
- Starting Python (Python)
- ASP.NET - session objects Problem (ASP.NET)
- Programming FAQ - Updated 1/March/2005 (Computer Science)
- simple random function (Python)
- How do I work with data files? (C#)
- Hopefully easy question about linking... (C++)
- more information about python (Python)
- Opening HTTP sessions with Python (Python)
Other Threads in the Python Forum
- Previous Thread: wxPython DateTime problems
- Next Thread: just a question..
| Thread Tools | Search this Thread |
advanced aliased bash beginner bits calling casino changecolor class clear command convert corners count csv cturtle cursor def definedlines dictionary digital dynamic dynamically events examples external file float format frange function google gui hints homework i/o iframe import info input java line linux list lists loop matching mouse multiple number numbers obexftp output parsing path port prime programming projects py py2exe pygame pygtk python random rational raw_input recursion return scrolledtext signal singleton skinning stderr string strings subprocess table tails terminal text thread threading time tkinter tlapse tuple tutorial ubuntu unicode urllib urllib2 valueerror variable voip web-scrape whileloop windows word wxpython






