Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 pop recursive to other variables
#1
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?
Gribouillis wrote Jun-30-2020, 04:31 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

I fixed for you this time. Please use code tags on future posts.
Quote
#2
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.
Quote
#3
(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:
[...]
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?

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).
Quote
#4
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
Quote

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)