# This statement means that x is now a reference to the object 4 >>> x = 4 # Set y to refer to what x refers to i.e. y is a reference to the object 4 as well >>> y = x # Set x to refer to the object 5 >>> x = 5 # x and y now have references to different objects >>> x != y True # Set orig to refer to a new list object, which has the values 2, 7, 12 >>> orig = [2, 7, 12] # Set cur to refer to what orig refers to, which is the same list object >>> cur = orig >>> print(orig) [2, 7, 12] >>> print(cur) [2, 7, 12] # Now pop the last value off of cur and print it >>> print(cur.pop()) 12 >>> print(cur) [2, 7] # But... >>> print(orig) [2, 7] # Because cur and orig both have a reference to the same object they are the same! >>> cur is orig True # The is operator is only True when both sides have a reference to the same object, # whereas == checks that the value of each side is equal: >>> False == 0 True >>> False is 0 False # If you want to be able to update the value of cur without affecting orig, you need them # to refer to different objects. So you make a *copy* of the list orig refers to, and then # set cur to refer to this new copy. # Using slice to get a new copy >>> cur = orig[:] # Using the copy module >>> import copy >>> cur = copy.copy(orig) # In both cases: >>> cur == orig True >>> cur is orig False # Something to keep aware of though is that both methods just copy the items in orig to # the new list, and so each list contains references to the same objects, i.e. >>> cur[0] is orig[0] True >>> cur[1] is orig[1] True # etc. So this can happen: >>> orig2 = ['x', 1.4, { 'f': 'Bob', 'l': 'Bobby' }] >>> cur2 = orig2[:] >>> cur2[0] = 'zzz' >>> cur2 ['zzz', 1.4, {'f': 'Bob', 'l': 'Bobby'}] >>> orig2 ['x', 1.4, {'f': 'Bob', 'l': 'Bobby'}] # But... >>> cur2[2]['f'] = 'Robert' >>> cur2 ['zzz', 1.4, {'f': 'Robert', 'l': 'Bobby'}] >>> orig2 ['x', 1.4, {'f': 'Robert', 'l': 'Bobby'}] # Both have been changed, because the last items of each list were referring to the same dictionary object! # To avoid, use the function deepcopy() in the copy module: >>> import copy >>> orig2 = ['x', 1.4, { 'f': 'Bob', 'l': 'Bobby' }] >>> cur2 = copy.deepcopy(orig2) >>> cur2[2]['f'] = 'Robert' >>> cur2 ['x', 1.4, {'f': 'Robert', 'l': 'Bobby'}] >>> orig2 ['x', 1.4, {'f': 'Bob', 'l': 'Bobby'}] # Hope that's helpful! If you're not using IPython for everything Python-related, do so - then I can just # type it into the console, then save the inputs and outputs for that to a file you could download and # load and re-run or edit in IPython yourself. # http://ipython.org/ipython-doc/stable/interactive/tutorial.html # https://splash.riverbed.com/community/product-lines/flyscript/blog/2013/03/08/interactive-python-coding-with-ipython # http://www.slideshare.net/ioggstream/will-ipythonreplacebash
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question