pop recursive to other variables - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: pop recursive to other variables (/thread-27994.html) |
pop recursive to other variables - nieman050313 - Jun-30-2020 Disclaimer: I am not a python guru, other languages quasi-guru. This isn't making sense to me, either I'm not fully understanding the pop method or possibly I have discovered a glitch??? Example of the way I expect variables to function: Python 3.8.2 (default, Apr 27 2020, 15:53:34) [GCC 9.3.0] on linux >>> x=1 >>> y=2 >>> z=3 >>> print(x,y,z) 1 2 3 >>> y=x >>> print(x,y,z) 1 1 3 >>> x=x+1 >>> print(x,y,z) 2 1 3 >>>Changing the variable X does not effect the variable Y in this example Now the issue: >>> color=['black','brown','green','yellow'] >>> print(color) ['black', 'brown', 'green', 'yellow'] >>> color2=color >>> print(color,color2) ['black', 'brown', 'green', 'yellow'] ['black', 'brown', 'green', 'yellow'] >>> colorpop=color2.pop() >>> print(color,color2,colorpop) ['black', 'brown', 'green'] ['black', 'brown', 'green'] yellow >>>var color2 received the list from var color var colorpop it the pop of var color2 but the pop of color2 recursively effects the var color as well Why are both var color and color2 "popped" Am I missing something here? RE: pop recursive to other variables - Gribouillis - Jun-30-2020 Suppose that you call your car Nitro and your partner calls it Batmobile. If I crash your car, it will affect both Nitro and Batmobile because they are just two different names for the same object. The same thing happens in python. If I say Batmobile = Nitro , I'm only defining a new name for the python object currently bearing the name Nitro. In your case, color and color2 are just two names for the same python list. If you pop that list, it will affect both variables.
RE: pop recursive to other variables - bowlofred - Jun-30-2020 (Jun-30-2020, 03:50 PM)nieman050313 Wrote: Changing the variable X does not effect the variable Y in this example That's because you are not "changing" the variable, you're assigning a new one. y=2 doesn't do anything to the variable that y held previously. It just makes a new assignment. A different object is now associated with the name "y" >>> y=3 >>> id(y) 4394573552 >>> y=2 >>> id(y) 4394573520 Quote:Now the issue: pop does modify the variable. The object remains in place, just the object is now modified.>>> color = ['white', 'blue', 'red'] >>> id(color) 4396729096 >>> color.pop() 'red' >>> id(color) 4396729096 >>> print(color) ['white', 'blue']Assignment doesn't change the data in an object, but other operations may (assuming the object is mutable). RE: pop recursive to other variables - nieman050313 - Jun-30-2020 ok......... bad morning......over-thinking....... first example I re-declared the x variable....... sorry for wasting your time, and sorry for missing the giant yellow banner at the top IRT posting with tags |