Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointers in Python? ` x.pointerDest = y.pointerDest`?

I am breaking my old question to parts because it is very messy beast here. This question is related to this answer and this answer. I try to understand pointers, not sure even whether they exist in Python.

# Won't change x with y=4
>>> x = 0; y = x; y = 4; x
0

# Won't change y
>>> x = 0; y = x; x = 2; y
0

#so how can I change pointers? Do they even exist in Python?
x = 0
y.pointerDestination = x.pointerDestination   #How? By which command?
x = 2
# y should be 0, how?

[Update 2: Solved]

Perhaps, contradictive statements about the existence There are no pointers in Python. and Python does not have the concept of a "pointer" to a simple scalar value.. Does the last one infer that there are pointers to something else, nullifying the first statement?

like image 597
hhh Avatar asked Oct 20 '25 07:10

hhh


1 Answers

Scalar objects in Python are immutable. If you use a non-scalar object, such as a list, you can do this:

>>> x = [0]
>>> y = x
>>> y[0] = 4
>>> y
[4]
>>> x
[4]
>>> x is y
True

Python does not have the concept of a "pointer" to a simple scalar value.

like image 54
Greg Hewgill Avatar answered Oct 21 '25 22:10

Greg Hewgill



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!