In Python, if I type a=1 b=2 c=a c=b, what is the value of c? What does c point to?
Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
Python variables are for the most part really easy to understand, but there is one case I have been struggling with. If I want to point my variable to a new memory address, how do I do this? Or, if Python does this by default (treating variables like pointers), then how do I literally assign the value from a new variable to the memory address of the old variable?
For example, if I type
a=1
b=2
c=a
c=b
What is the value of c
? And what does it point to? Is the statement replacing the pointer c -> a
with pointer c -> b
or grabbing the value from b
and overwriting a
with b
's value? c=b
is ambiguous.
In other words, if you start with this:
a -> 1 <- c
b -> 2
is it re-pointing c
like this:
a -> 1 _c
b -> 2 <-/
or copying b
like this?
a -> 2 <- c
b -> 2
python
add a comment |Â
up vote
6
down vote
favorite
Python variables are for the most part really easy to understand, but there is one case I have been struggling with. If I want to point my variable to a new memory address, how do I do this? Or, if Python does this by default (treating variables like pointers), then how do I literally assign the value from a new variable to the memory address of the old variable?
For example, if I type
a=1
b=2
c=a
c=b
What is the value of c
? And what does it point to? Is the statement replacing the pointer c -> a
with pointer c -> b
or grabbing the value from b
and overwriting a
with b
's value? c=b
is ambiguous.
In other words, if you start with this:
a -> 1 <- c
b -> 2
is it re-pointing c
like this:
a -> 1 _c
b -> 2 <-/
or copying b
like this?
a -> 2 <- c
b -> 2
python
4
In python you can reassign variables as you wish. c now refers to the same objects as b refers. Try thisprint(id(c) == id(b))
andprint(id(c) == id(a))
. Any similarity between c and a is now erased. See more about name here: docs.python.org/3/reference/executionmodel.html. I'm sure however there are more elaborate answers here on SO on the topic.
– Anton vBR
2 hours ago
2
Ok after seeing your update: name point at objects. they don't hold any values. no copying is involved.
– Anton vBR
2 hours ago
2
Read the following article by StackOverflow legend, Ned Batchelder, that explains this exhaustively: Facts and myths about Python names and values
– juanpa.arrivillaga
2 hours ago
@juanpa.arrivillaga good article. Even has a code runtime visualizer.
– Ryan
2 hours ago
@juanpa.arrivillaga Exactly the reference we needed. Any1 who has interest in understanding this question should be linked to that page.
– Anton vBR
2 hours ago
add a comment |Â
up vote
6
down vote
favorite
up vote
6
down vote
favorite
Python variables are for the most part really easy to understand, but there is one case I have been struggling with. If I want to point my variable to a new memory address, how do I do this? Or, if Python does this by default (treating variables like pointers), then how do I literally assign the value from a new variable to the memory address of the old variable?
For example, if I type
a=1
b=2
c=a
c=b
What is the value of c
? And what does it point to? Is the statement replacing the pointer c -> a
with pointer c -> b
or grabbing the value from b
and overwriting a
with b
's value? c=b
is ambiguous.
In other words, if you start with this:
a -> 1 <- c
b -> 2
is it re-pointing c
like this:
a -> 1 _c
b -> 2 <-/
or copying b
like this?
a -> 2 <- c
b -> 2
python
Python variables are for the most part really easy to understand, but there is one case I have been struggling with. If I want to point my variable to a new memory address, how do I do this? Or, if Python does this by default (treating variables like pointers), then how do I literally assign the value from a new variable to the memory address of the old variable?
For example, if I type
a=1
b=2
c=a
c=b
What is the value of c
? And what does it point to? Is the statement replacing the pointer c -> a
with pointer c -> b
or grabbing the value from b
and overwriting a
with b
's value? c=b
is ambiguous.
In other words, if you start with this:
a -> 1 <- c
b -> 2
is it re-pointing c
like this:
a -> 1 _c
b -> 2 <-/
or copying b
like this?
a -> 2 <- c
b -> 2
python
python
edited 2 hours ago
asked 2 hours ago
Ryan
777
777
4
In python you can reassign variables as you wish. c now refers to the same objects as b refers. Try thisprint(id(c) == id(b))
andprint(id(c) == id(a))
. Any similarity between c and a is now erased. See more about name here: docs.python.org/3/reference/executionmodel.html. I'm sure however there are more elaborate answers here on SO on the topic.
– Anton vBR
2 hours ago
2
Ok after seeing your update: name point at objects. they don't hold any values. no copying is involved.
– Anton vBR
2 hours ago
2
Read the following article by StackOverflow legend, Ned Batchelder, that explains this exhaustively: Facts and myths about Python names and values
– juanpa.arrivillaga
2 hours ago
@juanpa.arrivillaga good article. Even has a code runtime visualizer.
– Ryan
2 hours ago
@juanpa.arrivillaga Exactly the reference we needed. Any1 who has interest in understanding this question should be linked to that page.
– Anton vBR
2 hours ago
add a comment |Â
4
In python you can reassign variables as you wish. c now refers to the same objects as b refers. Try thisprint(id(c) == id(b))
andprint(id(c) == id(a))
. Any similarity between c and a is now erased. See more about name here: docs.python.org/3/reference/executionmodel.html. I'm sure however there are more elaborate answers here on SO on the topic.
– Anton vBR
2 hours ago
2
Ok after seeing your update: name point at objects. they don't hold any values. no copying is involved.
– Anton vBR
2 hours ago
2
Read the following article by StackOverflow legend, Ned Batchelder, that explains this exhaustively: Facts and myths about Python names and values
– juanpa.arrivillaga
2 hours ago
@juanpa.arrivillaga good article. Even has a code runtime visualizer.
– Ryan
2 hours ago
@juanpa.arrivillaga Exactly the reference we needed. Any1 who has interest in understanding this question should be linked to that page.
– Anton vBR
2 hours ago
4
4
In python you can reassign variables as you wish. c now refers to the same objects as b refers. Try this
print(id(c) == id(b))
and print(id(c) == id(a))
. Any similarity between c and a is now erased. See more about name here: docs.python.org/3/reference/executionmodel.html. I'm sure however there are more elaborate answers here on SO on the topic.– Anton vBR
2 hours ago
In python you can reassign variables as you wish. c now refers to the same objects as b refers. Try this
print(id(c) == id(b))
and print(id(c) == id(a))
. Any similarity between c and a is now erased. See more about name here: docs.python.org/3/reference/executionmodel.html. I'm sure however there are more elaborate answers here on SO on the topic.– Anton vBR
2 hours ago
2
2
Ok after seeing your update: name point at objects. they don't hold any values. no copying is involved.
– Anton vBR
2 hours ago
Ok after seeing your update: name point at objects. they don't hold any values. no copying is involved.
– Anton vBR
2 hours ago
2
2
Read the following article by StackOverflow legend, Ned Batchelder, that explains this exhaustively: Facts and myths about Python names and values
– juanpa.arrivillaga
2 hours ago
Read the following article by StackOverflow legend, Ned Batchelder, that explains this exhaustively: Facts and myths about Python names and values
– juanpa.arrivillaga
2 hours ago
@juanpa.arrivillaga good article. Even has a code runtime visualizer.
– Ryan
2 hours ago
@juanpa.arrivillaga good article. Even has a code runtime visualizer.
– Ryan
2 hours ago
@juanpa.arrivillaga Exactly the reference we needed. Any1 who has interest in understanding this question should be linked to that page.
– Anton vBR
2 hours ago
@juanpa.arrivillaga Exactly the reference we needed. Any1 who has interest in understanding this question should be linked to that page.
– Anton vBR
2 hours ago
add a comment |Â
7 Answers
7
active
oldest
votes
up vote
1
down vote
accepted
Well, in your code:
a=1
b=2
c=a
c=b
Before you assign c
to b
and after you assign c
to a
, c
will be a
.
And after, at the end of the code, c
will be b
, because you're reassigning the variable.
The second assignation, basically creates new variable, without knowing that the variable is there already, so it will just do it but no way of accessing previous holding-value of the varaible
Thanks. I like your answer, because it shows how different Python's execution model is from C++. In your explanation, when you say "assignc
tob
", a C++ developer would look at this and say it is backwards. They would "assignb
toc
", because the assignment operator is a very low-level operation with an implied flow/direction for the data (retrieve value ofb
, malloc to memory heap, and dump the CPU's register into the memory location atc
).
– Ryan
1 hour ago
From now on, this is how I will be thinking about it.
– Ryan
1 hour ago
1
I actually think this answer is a little misleading, since it says things like "c
will bea
", but it's really not true thatc
isa
; instead,c
"is" the same value thata
also happens to be. There's no relation betweenc
anda
, except that (at a particular point in the program's execution) they happen to both refer to the same value. @Ryan, if this explanation really helps you understand how Python actually works, that's fine, but I think it has the potential to confuse other people in the same way you were confused when you asked the question.
– David Z
50 mins ago
1
@Ryan in C terms, I suppose, you could say that in Python, you can never really refer to a value of an object directly, just indirectly through a pointer which you cannot dereference. You can merely swap pointers around on a symbol table.
– juanpa.arrivillaga
19 mins ago
@DavidZ This wording is specifically perfect for helping a low level language programmer understand Python's confusing execution model. The inaccuracy you see in his wording is actually helpful for tipping the C programmer brain off that this is not the same. It kicks the C programmer brain out of C mode while still wording it in a way that makes sense to a C programmer brain, because the C programmer brain will automatically resolvea
andb
to their actual memory locations. It is only misleading to a non-C programmer haha.
– Ryan
6 mins ago
add a comment |Â
up vote
10
down vote
There are no pointers to variables in Python. In particular, when you say this:
Is the statement replacing the pointer
c -> a
with pointerc -> b
...
Python does not have any such thing as "the pointer c -> a
", so it is not doing that.
...or grabbing the value from b and overwriting a with b's value
but there is no assignment to a
, so it's not doing that either.
Instead, Python keeps a symbol table1 that maps each name (a
, b
, c
, etc.) to a pointer to an object. In your code sample, after you assign to a
and b
, it would look like this (obviously I have made up the memory addresses):
a -> 0xfffa9600 -> 1
b -> 0xfffa9608 -> 2
and then after you assign c = a
, it would look like this:
a -> 0xfffa9600 -> 1
b -> 0xfffa9608 -> 2
c -> 0xfffa9600 -> 1
Note that c
is entirely independent of a
. When you run c = b
, it replaces the pointer associated with c
in the symbol table with the pointer that was associated with b
, but a
is not affected:
a -> 0xfffa9600 -> 1
b -> 0xfffa9608 -> 2
c -> 0xfffa9608 -> 2
In this case that's pretty much all there is to it because the objects in question, namely the integer constants 1
and 2
, are immutable. However, if you use mutable objects, they do start to act a bit more like pointers in the sense that changes to the object when it's stored in one variable are reflected in other variables that refer to the same object. For example, consider this sample of code:
x = 'a': 1, 'b': 2
y = x
Here, the symbol table might look something like this:
x -> 0xffdc1040 -> 'a': 1, 'b': 2
y -> 0xffdc1040 -> 'a': 1, 'b': 2
If you now run
y['b'] = y['a']
then it doesn't actually change the pointer associated with y
in the symbol table, but it does change the object pointed to by that pointer, so you wind up with
x -> 0xffdc1040 -> 'a': 1, 'b': 1
y -> 0xffdc1040 -> 'a': 1, 'b': 1
and you'll see that your assignment to y['b']
has affected x
as well. Contrast this with
y = 'a': 1, 'b': 2
which actually makes y
point at an entirely different object, and is more akin to what you were doing before with a
, b
, and c
.
1Actually there are several symbol tables, corresponding to different scopes, and Python has an order in which it checks them, but that detail isn't particularly relevant here.
Nice! Are these memory addresses equal to the ids of the objects?
– Anton vBR
1 hour ago
Very in depth. Thank you. As someone else pointed out I was not clear in my explanation. When I wrotec -> a
, in my mind I was resolvinga
to a physical location in memory storing the value 1. It seems that Python has an added layer of resolution. My background in C++ causes confusion for me in Python.
– Ryan
1 hour ago
2
@AntonvBR If by "ids" you mean the values returned from theid()
builtin function: they might be. In the standard Python interpreter (CPython), at least currently,id()
does give the memory address. Other Python interpreters (or future versions of CPython) might do it differently.
– David Z
1 hour ago
@Ryan You're welcome, glad I could help. This is a common source of confusion for people coming to Python from lower level languages so you'll find a lot written about it on Stack Overflow and elsewhere.
– David Z
1 hour ago
add a comment |Â
up vote
4
down vote
c
doesn't "Point at a
or b
"... it points at the 1
or 2
objects.
>>> a = 1
>>> b = 2
>>> c = a
>>> c
1
>>> c = b
>>> c
2
>>> b = 3
>>> c
2
This can be proven somewhat with id()
- b
and c
point at the same "thing":
>>> b = 2
>>> c = b
>>> id(b)
42766656
>>> id(c)
42766656
Your answer is a good balance between in depth and easy to understand. It is critical for C++ developers like me to realize that the value is an object separate from the labelc
.c
IS a memory location that contains a pointer to another memory location that is called1
, but in C++,c
IS a memory location that contains data that is1
.
– Ryan
1 hour ago
add a comment |Â
up vote
2
down vote
Answering both of your question at once What is the value of c? What does c point to?, I've added an step by step execution with the id()
of each variable with proper comment. Hope this helps you understand properly what is happening under the hood.
>>> a=1
>>> b=2
>>> print(id(a))
1574071312 # this is the address of a
>>> print(id(b))
1574071344 # this is the address of b
>>>c=a # assignment of a to c
>>> print(c)
1 # c will contain now the value of a
>>> print(id(c))
1574071312 # this is the address of c which is same as a
>>> c=b # re-assignment of b to c
>>> print(c)
2 # c wil contain now the value of b
>>> print(id(c))
1574071344 # this the address of c now which is same as b
add a comment |Â
up vote
1
down vote
Well my friend, in this example if c
is pointing to a
, it'll appear like they are pointing at the same value but not, for example if you're pointing
a = 2
c = a
then after this declaration, if you change the value of a = 3
, c
will change it's value to 3
also.
Imagine those variables values [2][3]
in boxes and the variables a, b, c
are just pointing to those boxes.
If one variable a
is pointing to one box, and the other variable c
is pointing to the variable a
that is pointing to the box [2]
, the last variable c
is just following the first variable a
, not the value [2]
, hope this explanation gets you happy.
New contributor
Joaquim Mucage is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Please edit your question, using formatting makes it easier readable
– Bert Verhees
2 hours ago
This is a good way to explain it to new programmers. The [ ] square brackets visualization is very helpful.
– Ryan
45 mins ago
add a comment |Â
up vote
0
down vote
Basically, in the fourth line c variable is being overwritten by the value of b. As this is the last statement, c will hold the value of 2.
How do I explicitly force b's value to overwrite/copy to a's value?
– Ryan
2 hours ago
2
@Ryan in Python, you do not. Variables act like name-tags to objects, they are not memory addresses. Python doesn't have pointers.
– juanpa.arrivillaga
2 hours ago
This explanation is ambiguous, because it relies on me already understanding that the "c variable" is its own memory location pointing to another memory location.
– Ryan
1 hour ago
add a comment |Â
up vote
0
down vote
So to summarize a few of the really good answers I saw from others,
- "values" are the variables, themselves, and are objects without a name.
- Names of variables are "labels" that are their own separate objects, and they can point to any value objects.
- The Assignment operator points a label object to a value object.
Let's inaccurately go step by step through the assignment operation from the point of view of the Python interpreter:
First, we create a value.
[value obj]
Note: [ ] denotes a physical memory location. This means the value has its own unique memory address.
Next, we create a label.
[Label obj] -> nothing
Last, we assign the label to its value.
[Label obj] -> [value obj]
So,
a = 1
is the same as
[memorylocation containing "a"] -> [memorylocation containing 1]
and
c = b
is same as
[memorylocation containing "c"] -> "b" resolved to [memorylocation containing 2]
1
I think "variable object" and "name object" is just confusing. In Python, a namespace is quite literally adict
.
– juanpa.arrivillaga
24 mins ago
add a comment |Â
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Well, in your code:
a=1
b=2
c=a
c=b
Before you assign c
to b
and after you assign c
to a
, c
will be a
.
And after, at the end of the code, c
will be b
, because you're reassigning the variable.
The second assignation, basically creates new variable, without knowing that the variable is there already, so it will just do it but no way of accessing previous holding-value of the varaible
Thanks. I like your answer, because it shows how different Python's execution model is from C++. In your explanation, when you say "assignc
tob
", a C++ developer would look at this and say it is backwards. They would "assignb
toc
", because the assignment operator is a very low-level operation with an implied flow/direction for the data (retrieve value ofb
, malloc to memory heap, and dump the CPU's register into the memory location atc
).
– Ryan
1 hour ago
From now on, this is how I will be thinking about it.
– Ryan
1 hour ago
1
I actually think this answer is a little misleading, since it says things like "c
will bea
", but it's really not true thatc
isa
; instead,c
"is" the same value thata
also happens to be. There's no relation betweenc
anda
, except that (at a particular point in the program's execution) they happen to both refer to the same value. @Ryan, if this explanation really helps you understand how Python actually works, that's fine, but I think it has the potential to confuse other people in the same way you were confused when you asked the question.
– David Z
50 mins ago
1
@Ryan in C terms, I suppose, you could say that in Python, you can never really refer to a value of an object directly, just indirectly through a pointer which you cannot dereference. You can merely swap pointers around on a symbol table.
– juanpa.arrivillaga
19 mins ago
@DavidZ This wording is specifically perfect for helping a low level language programmer understand Python's confusing execution model. The inaccuracy you see in his wording is actually helpful for tipping the C programmer brain off that this is not the same. It kicks the C programmer brain out of C mode while still wording it in a way that makes sense to a C programmer brain, because the C programmer brain will automatically resolvea
andb
to their actual memory locations. It is only misleading to a non-C programmer haha.
– Ryan
6 mins ago
add a comment |Â
up vote
1
down vote
accepted
Well, in your code:
a=1
b=2
c=a
c=b
Before you assign c
to b
and after you assign c
to a
, c
will be a
.
And after, at the end of the code, c
will be b
, because you're reassigning the variable.
The second assignation, basically creates new variable, without knowing that the variable is there already, so it will just do it but no way of accessing previous holding-value of the varaible
Thanks. I like your answer, because it shows how different Python's execution model is from C++. In your explanation, when you say "assignc
tob
", a C++ developer would look at this and say it is backwards. They would "assignb
toc
", because the assignment operator is a very low-level operation with an implied flow/direction for the data (retrieve value ofb
, malloc to memory heap, and dump the CPU's register into the memory location atc
).
– Ryan
1 hour ago
From now on, this is how I will be thinking about it.
– Ryan
1 hour ago
1
I actually think this answer is a little misleading, since it says things like "c
will bea
", but it's really not true thatc
isa
; instead,c
"is" the same value thata
also happens to be. There's no relation betweenc
anda
, except that (at a particular point in the program's execution) they happen to both refer to the same value. @Ryan, if this explanation really helps you understand how Python actually works, that's fine, but I think it has the potential to confuse other people in the same way you were confused when you asked the question.
– David Z
50 mins ago
1
@Ryan in C terms, I suppose, you could say that in Python, you can never really refer to a value of an object directly, just indirectly through a pointer which you cannot dereference. You can merely swap pointers around on a symbol table.
– juanpa.arrivillaga
19 mins ago
@DavidZ This wording is specifically perfect for helping a low level language programmer understand Python's confusing execution model. The inaccuracy you see in his wording is actually helpful for tipping the C programmer brain off that this is not the same. It kicks the C programmer brain out of C mode while still wording it in a way that makes sense to a C programmer brain, because the C programmer brain will automatically resolvea
andb
to their actual memory locations. It is only misleading to a non-C programmer haha.
– Ryan
6 mins ago
add a comment |Â
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Well, in your code:
a=1
b=2
c=a
c=b
Before you assign c
to b
and after you assign c
to a
, c
will be a
.
And after, at the end of the code, c
will be b
, because you're reassigning the variable.
The second assignation, basically creates new variable, without knowing that the variable is there already, so it will just do it but no way of accessing previous holding-value of the varaible
Well, in your code:
a=1
b=2
c=a
c=b
Before you assign c
to b
and after you assign c
to a
, c
will be a
.
And after, at the end of the code, c
will be b
, because you're reassigning the variable.
The second assignation, basically creates new variable, without knowing that the variable is there already, so it will just do it but no way of accessing previous holding-value of the varaible
answered 2 hours ago


U9-Forward
6,4682629
6,4682629
Thanks. I like your answer, because it shows how different Python's execution model is from C++. In your explanation, when you say "assignc
tob
", a C++ developer would look at this and say it is backwards. They would "assignb
toc
", because the assignment operator is a very low-level operation with an implied flow/direction for the data (retrieve value ofb
, malloc to memory heap, and dump the CPU's register into the memory location atc
).
– Ryan
1 hour ago
From now on, this is how I will be thinking about it.
– Ryan
1 hour ago
1
I actually think this answer is a little misleading, since it says things like "c
will bea
", but it's really not true thatc
isa
; instead,c
"is" the same value thata
also happens to be. There's no relation betweenc
anda
, except that (at a particular point in the program's execution) they happen to both refer to the same value. @Ryan, if this explanation really helps you understand how Python actually works, that's fine, but I think it has the potential to confuse other people in the same way you were confused when you asked the question.
– David Z
50 mins ago
1
@Ryan in C terms, I suppose, you could say that in Python, you can never really refer to a value of an object directly, just indirectly through a pointer which you cannot dereference. You can merely swap pointers around on a symbol table.
– juanpa.arrivillaga
19 mins ago
@DavidZ This wording is specifically perfect for helping a low level language programmer understand Python's confusing execution model. The inaccuracy you see in his wording is actually helpful for tipping the C programmer brain off that this is not the same. It kicks the C programmer brain out of C mode while still wording it in a way that makes sense to a C programmer brain, because the C programmer brain will automatically resolvea
andb
to their actual memory locations. It is only misleading to a non-C programmer haha.
– Ryan
6 mins ago
add a comment |Â
Thanks. I like your answer, because it shows how different Python's execution model is from C++. In your explanation, when you say "assignc
tob
", a C++ developer would look at this and say it is backwards. They would "assignb
toc
", because the assignment operator is a very low-level operation with an implied flow/direction for the data (retrieve value ofb
, malloc to memory heap, and dump the CPU's register into the memory location atc
).
– Ryan
1 hour ago
From now on, this is how I will be thinking about it.
– Ryan
1 hour ago
1
I actually think this answer is a little misleading, since it says things like "c
will bea
", but it's really not true thatc
isa
; instead,c
"is" the same value thata
also happens to be. There's no relation betweenc
anda
, except that (at a particular point in the program's execution) they happen to both refer to the same value. @Ryan, if this explanation really helps you understand how Python actually works, that's fine, but I think it has the potential to confuse other people in the same way you were confused when you asked the question.
– David Z
50 mins ago
1
@Ryan in C terms, I suppose, you could say that in Python, you can never really refer to a value of an object directly, just indirectly through a pointer which you cannot dereference. You can merely swap pointers around on a symbol table.
– juanpa.arrivillaga
19 mins ago
@DavidZ This wording is specifically perfect for helping a low level language programmer understand Python's confusing execution model. The inaccuracy you see in his wording is actually helpful for tipping the C programmer brain off that this is not the same. It kicks the C programmer brain out of C mode while still wording it in a way that makes sense to a C programmer brain, because the C programmer brain will automatically resolvea
andb
to their actual memory locations. It is only misleading to a non-C programmer haha.
– Ryan
6 mins ago
Thanks. I like your answer, because it shows how different Python's execution model is from C++. In your explanation, when you say "assign
c
to b
", a C++ developer would look at this and say it is backwards. They would "assign b
to c
", because the assignment operator is a very low-level operation with an implied flow/direction for the data (retrieve value of b
, malloc to memory heap, and dump the CPU's register into the memory location at c
).– Ryan
1 hour ago
Thanks. I like your answer, because it shows how different Python's execution model is from C++. In your explanation, when you say "assign
c
to b
", a C++ developer would look at this and say it is backwards. They would "assign b
to c
", because the assignment operator is a very low-level operation with an implied flow/direction for the data (retrieve value of b
, malloc to memory heap, and dump the CPU's register into the memory location at c
).– Ryan
1 hour ago
From now on, this is how I will be thinking about it.
– Ryan
1 hour ago
From now on, this is how I will be thinking about it.
– Ryan
1 hour ago
1
1
I actually think this answer is a little misleading, since it says things like "
c
will be a
", but it's really not true that c
is a
; instead, c
"is" the same value that a
also happens to be. There's no relation between c
and a
, except that (at a particular point in the program's execution) they happen to both refer to the same value. @Ryan, if this explanation really helps you understand how Python actually works, that's fine, but I think it has the potential to confuse other people in the same way you were confused when you asked the question.– David Z
50 mins ago
I actually think this answer is a little misleading, since it says things like "
c
will be a
", but it's really not true that c
is a
; instead, c
"is" the same value that a
also happens to be. There's no relation between c
and a
, except that (at a particular point in the program's execution) they happen to both refer to the same value. @Ryan, if this explanation really helps you understand how Python actually works, that's fine, but I think it has the potential to confuse other people in the same way you were confused when you asked the question.– David Z
50 mins ago
1
1
@Ryan in C terms, I suppose, you could say that in Python, you can never really refer to a value of an object directly, just indirectly through a pointer which you cannot dereference. You can merely swap pointers around on a symbol table.
– juanpa.arrivillaga
19 mins ago
@Ryan in C terms, I suppose, you could say that in Python, you can never really refer to a value of an object directly, just indirectly through a pointer which you cannot dereference. You can merely swap pointers around on a symbol table.
– juanpa.arrivillaga
19 mins ago
@DavidZ This wording is specifically perfect for helping a low level language programmer understand Python's confusing execution model. The inaccuracy you see in his wording is actually helpful for tipping the C programmer brain off that this is not the same. It kicks the C programmer brain out of C mode while still wording it in a way that makes sense to a C programmer brain, because the C programmer brain will automatically resolve
a
and b
to their actual memory locations. It is only misleading to a non-C programmer haha.– Ryan
6 mins ago
@DavidZ This wording is specifically perfect for helping a low level language programmer understand Python's confusing execution model. The inaccuracy you see in his wording is actually helpful for tipping the C programmer brain off that this is not the same. It kicks the C programmer brain out of C mode while still wording it in a way that makes sense to a C programmer brain, because the C programmer brain will automatically resolve
a
and b
to their actual memory locations. It is only misleading to a non-C programmer haha.– Ryan
6 mins ago
add a comment |Â
up vote
10
down vote
There are no pointers to variables in Python. In particular, when you say this:
Is the statement replacing the pointer
c -> a
with pointerc -> b
...
Python does not have any such thing as "the pointer c -> a
", so it is not doing that.
...or grabbing the value from b and overwriting a with b's value
but there is no assignment to a
, so it's not doing that either.
Instead, Python keeps a symbol table1 that maps each name (a
, b
, c
, etc.) to a pointer to an object. In your code sample, after you assign to a
and b
, it would look like this (obviously I have made up the memory addresses):
a -> 0xfffa9600 -> 1
b -> 0xfffa9608 -> 2
and then after you assign c = a
, it would look like this:
a -> 0xfffa9600 -> 1
b -> 0xfffa9608 -> 2
c -> 0xfffa9600 -> 1
Note that c
is entirely independent of a
. When you run c = b
, it replaces the pointer associated with c
in the symbol table with the pointer that was associated with b
, but a
is not affected:
a -> 0xfffa9600 -> 1
b -> 0xfffa9608 -> 2
c -> 0xfffa9608 -> 2
In this case that's pretty much all there is to it because the objects in question, namely the integer constants 1
and 2
, are immutable. However, if you use mutable objects, they do start to act a bit more like pointers in the sense that changes to the object when it's stored in one variable are reflected in other variables that refer to the same object. For example, consider this sample of code:
x = 'a': 1, 'b': 2
y = x
Here, the symbol table might look something like this:
x -> 0xffdc1040 -> 'a': 1, 'b': 2
y -> 0xffdc1040 -> 'a': 1, 'b': 2
If you now run
y['b'] = y['a']
then it doesn't actually change the pointer associated with y
in the symbol table, but it does change the object pointed to by that pointer, so you wind up with
x -> 0xffdc1040 -> 'a': 1, 'b': 1
y -> 0xffdc1040 -> 'a': 1, 'b': 1
and you'll see that your assignment to y['b']
has affected x
as well. Contrast this with
y = 'a': 1, 'b': 2
which actually makes y
point at an entirely different object, and is more akin to what you were doing before with a
, b
, and c
.
1Actually there are several symbol tables, corresponding to different scopes, and Python has an order in which it checks them, but that detail isn't particularly relevant here.
Nice! Are these memory addresses equal to the ids of the objects?
– Anton vBR
1 hour ago
Very in depth. Thank you. As someone else pointed out I was not clear in my explanation. When I wrotec -> a
, in my mind I was resolvinga
to a physical location in memory storing the value 1. It seems that Python has an added layer of resolution. My background in C++ causes confusion for me in Python.
– Ryan
1 hour ago
2
@AntonvBR If by "ids" you mean the values returned from theid()
builtin function: they might be. In the standard Python interpreter (CPython), at least currently,id()
does give the memory address. Other Python interpreters (or future versions of CPython) might do it differently.
– David Z
1 hour ago
@Ryan You're welcome, glad I could help. This is a common source of confusion for people coming to Python from lower level languages so you'll find a lot written about it on Stack Overflow and elsewhere.
– David Z
1 hour ago
add a comment |Â
up vote
10
down vote
There are no pointers to variables in Python. In particular, when you say this:
Is the statement replacing the pointer
c -> a
with pointerc -> b
...
Python does not have any such thing as "the pointer c -> a
", so it is not doing that.
...or grabbing the value from b and overwriting a with b's value
but there is no assignment to a
, so it's not doing that either.
Instead, Python keeps a symbol table1 that maps each name (a
, b
, c
, etc.) to a pointer to an object. In your code sample, after you assign to a
and b
, it would look like this (obviously I have made up the memory addresses):
a -> 0xfffa9600 -> 1
b -> 0xfffa9608 -> 2
and then after you assign c = a
, it would look like this:
a -> 0xfffa9600 -> 1
b -> 0xfffa9608 -> 2
c -> 0xfffa9600 -> 1
Note that c
is entirely independent of a
. When you run c = b
, it replaces the pointer associated with c
in the symbol table with the pointer that was associated with b
, but a
is not affected:
a -> 0xfffa9600 -> 1
b -> 0xfffa9608 -> 2
c -> 0xfffa9608 -> 2
In this case that's pretty much all there is to it because the objects in question, namely the integer constants 1
and 2
, are immutable. However, if you use mutable objects, they do start to act a bit more like pointers in the sense that changes to the object when it's stored in one variable are reflected in other variables that refer to the same object. For example, consider this sample of code:
x = 'a': 1, 'b': 2
y = x
Here, the symbol table might look something like this:
x -> 0xffdc1040 -> 'a': 1, 'b': 2
y -> 0xffdc1040 -> 'a': 1, 'b': 2
If you now run
y['b'] = y['a']
then it doesn't actually change the pointer associated with y
in the symbol table, but it does change the object pointed to by that pointer, so you wind up with
x -> 0xffdc1040 -> 'a': 1, 'b': 1
y -> 0xffdc1040 -> 'a': 1, 'b': 1
and you'll see that your assignment to y['b']
has affected x
as well. Contrast this with
y = 'a': 1, 'b': 2
which actually makes y
point at an entirely different object, and is more akin to what you were doing before with a
, b
, and c
.
1Actually there are several symbol tables, corresponding to different scopes, and Python has an order in which it checks them, but that detail isn't particularly relevant here.
Nice! Are these memory addresses equal to the ids of the objects?
– Anton vBR
1 hour ago
Very in depth. Thank you. As someone else pointed out I was not clear in my explanation. When I wrotec -> a
, in my mind I was resolvinga
to a physical location in memory storing the value 1. It seems that Python has an added layer of resolution. My background in C++ causes confusion for me in Python.
– Ryan
1 hour ago
2
@AntonvBR If by "ids" you mean the values returned from theid()
builtin function: they might be. In the standard Python interpreter (CPython), at least currently,id()
does give the memory address. Other Python interpreters (or future versions of CPython) might do it differently.
– David Z
1 hour ago
@Ryan You're welcome, glad I could help. This is a common source of confusion for people coming to Python from lower level languages so you'll find a lot written about it on Stack Overflow and elsewhere.
– David Z
1 hour ago
add a comment |Â
up vote
10
down vote
up vote
10
down vote
There are no pointers to variables in Python. In particular, when you say this:
Is the statement replacing the pointer
c -> a
with pointerc -> b
...
Python does not have any such thing as "the pointer c -> a
", so it is not doing that.
...or grabbing the value from b and overwriting a with b's value
but there is no assignment to a
, so it's not doing that either.
Instead, Python keeps a symbol table1 that maps each name (a
, b
, c
, etc.) to a pointer to an object. In your code sample, after you assign to a
and b
, it would look like this (obviously I have made up the memory addresses):
a -> 0xfffa9600 -> 1
b -> 0xfffa9608 -> 2
and then after you assign c = a
, it would look like this:
a -> 0xfffa9600 -> 1
b -> 0xfffa9608 -> 2
c -> 0xfffa9600 -> 1
Note that c
is entirely independent of a
. When you run c = b
, it replaces the pointer associated with c
in the symbol table with the pointer that was associated with b
, but a
is not affected:
a -> 0xfffa9600 -> 1
b -> 0xfffa9608 -> 2
c -> 0xfffa9608 -> 2
In this case that's pretty much all there is to it because the objects in question, namely the integer constants 1
and 2
, are immutable. However, if you use mutable objects, they do start to act a bit more like pointers in the sense that changes to the object when it's stored in one variable are reflected in other variables that refer to the same object. For example, consider this sample of code:
x = 'a': 1, 'b': 2
y = x
Here, the symbol table might look something like this:
x -> 0xffdc1040 -> 'a': 1, 'b': 2
y -> 0xffdc1040 -> 'a': 1, 'b': 2
If you now run
y['b'] = y['a']
then it doesn't actually change the pointer associated with y
in the symbol table, but it does change the object pointed to by that pointer, so you wind up with
x -> 0xffdc1040 -> 'a': 1, 'b': 1
y -> 0xffdc1040 -> 'a': 1, 'b': 1
and you'll see that your assignment to y['b']
has affected x
as well. Contrast this with
y = 'a': 1, 'b': 2
which actually makes y
point at an entirely different object, and is more akin to what you were doing before with a
, b
, and c
.
1Actually there are several symbol tables, corresponding to different scopes, and Python has an order in which it checks them, but that detail isn't particularly relevant here.
There are no pointers to variables in Python. In particular, when you say this:
Is the statement replacing the pointer
c -> a
with pointerc -> b
...
Python does not have any such thing as "the pointer c -> a
", so it is not doing that.
...or grabbing the value from b and overwriting a with b's value
but there is no assignment to a
, so it's not doing that either.
Instead, Python keeps a symbol table1 that maps each name (a
, b
, c
, etc.) to a pointer to an object. In your code sample, after you assign to a
and b
, it would look like this (obviously I have made up the memory addresses):
a -> 0xfffa9600 -> 1
b -> 0xfffa9608 -> 2
and then after you assign c = a
, it would look like this:
a -> 0xfffa9600 -> 1
b -> 0xfffa9608 -> 2
c -> 0xfffa9600 -> 1
Note that c
is entirely independent of a
. When you run c = b
, it replaces the pointer associated with c
in the symbol table with the pointer that was associated with b
, but a
is not affected:
a -> 0xfffa9600 -> 1
b -> 0xfffa9608 -> 2
c -> 0xfffa9608 -> 2
In this case that's pretty much all there is to it because the objects in question, namely the integer constants 1
and 2
, are immutable. However, if you use mutable objects, they do start to act a bit more like pointers in the sense that changes to the object when it's stored in one variable are reflected in other variables that refer to the same object. For example, consider this sample of code:
x = 'a': 1, 'b': 2
y = x
Here, the symbol table might look something like this:
x -> 0xffdc1040 -> 'a': 1, 'b': 2
y -> 0xffdc1040 -> 'a': 1, 'b': 2
If you now run
y['b'] = y['a']
then it doesn't actually change the pointer associated with y
in the symbol table, but it does change the object pointed to by that pointer, so you wind up with
x -> 0xffdc1040 -> 'a': 1, 'b': 1
y -> 0xffdc1040 -> 'a': 1, 'b': 1
and you'll see that your assignment to y['b']
has affected x
as well. Contrast this with
y = 'a': 1, 'b': 2
which actually makes y
point at an entirely different object, and is more akin to what you were doing before with a
, b
, and c
.
1Actually there are several symbol tables, corresponding to different scopes, and Python has an order in which it checks them, but that detail isn't particularly relevant here.
edited 2 hours ago
answered 2 hours ago


David Z
92k17196232
92k17196232
Nice! Are these memory addresses equal to the ids of the objects?
– Anton vBR
1 hour ago
Very in depth. Thank you. As someone else pointed out I was not clear in my explanation. When I wrotec -> a
, in my mind I was resolvinga
to a physical location in memory storing the value 1. It seems that Python has an added layer of resolution. My background in C++ causes confusion for me in Python.
– Ryan
1 hour ago
2
@AntonvBR If by "ids" you mean the values returned from theid()
builtin function: they might be. In the standard Python interpreter (CPython), at least currently,id()
does give the memory address. Other Python interpreters (or future versions of CPython) might do it differently.
– David Z
1 hour ago
@Ryan You're welcome, glad I could help. This is a common source of confusion for people coming to Python from lower level languages so you'll find a lot written about it on Stack Overflow and elsewhere.
– David Z
1 hour ago
add a comment |Â
Nice! Are these memory addresses equal to the ids of the objects?
– Anton vBR
1 hour ago
Very in depth. Thank you. As someone else pointed out I was not clear in my explanation. When I wrotec -> a
, in my mind I was resolvinga
to a physical location in memory storing the value 1. It seems that Python has an added layer of resolution. My background in C++ causes confusion for me in Python.
– Ryan
1 hour ago
2
@AntonvBR If by "ids" you mean the values returned from theid()
builtin function: they might be. In the standard Python interpreter (CPython), at least currently,id()
does give the memory address. Other Python interpreters (or future versions of CPython) might do it differently.
– David Z
1 hour ago
@Ryan You're welcome, glad I could help. This is a common source of confusion for people coming to Python from lower level languages so you'll find a lot written about it on Stack Overflow and elsewhere.
– David Z
1 hour ago
Nice! Are these memory addresses equal to the ids of the objects?
– Anton vBR
1 hour ago
Nice! Are these memory addresses equal to the ids of the objects?
– Anton vBR
1 hour ago
Very in depth. Thank you. As someone else pointed out I was not clear in my explanation. When I wrote
c -> a
, in my mind I was resolving a
to a physical location in memory storing the value 1. It seems that Python has an added layer of resolution. My background in C++ causes confusion for me in Python.– Ryan
1 hour ago
Very in depth. Thank you. As someone else pointed out I was not clear in my explanation. When I wrote
c -> a
, in my mind I was resolving a
to a physical location in memory storing the value 1. It seems that Python has an added layer of resolution. My background in C++ causes confusion for me in Python.– Ryan
1 hour ago
2
2
@AntonvBR If by "ids" you mean the values returned from the
id()
builtin function: they might be. In the standard Python interpreter (CPython), at least currently, id()
does give the memory address. Other Python interpreters (or future versions of CPython) might do it differently.– David Z
1 hour ago
@AntonvBR If by "ids" you mean the values returned from the
id()
builtin function: they might be. In the standard Python interpreter (CPython), at least currently, id()
does give the memory address. Other Python interpreters (or future versions of CPython) might do it differently.– David Z
1 hour ago
@Ryan You're welcome, glad I could help. This is a common source of confusion for people coming to Python from lower level languages so you'll find a lot written about it on Stack Overflow and elsewhere.
– David Z
1 hour ago
@Ryan You're welcome, glad I could help. This is a common source of confusion for people coming to Python from lower level languages so you'll find a lot written about it on Stack Overflow and elsewhere.
– David Z
1 hour ago
add a comment |Â
up vote
4
down vote
c
doesn't "Point at a
or b
"... it points at the 1
or 2
objects.
>>> a = 1
>>> b = 2
>>> c = a
>>> c
1
>>> c = b
>>> c
2
>>> b = 3
>>> c
2
This can be proven somewhat with id()
- b
and c
point at the same "thing":
>>> b = 2
>>> c = b
>>> id(b)
42766656
>>> id(c)
42766656
Your answer is a good balance between in depth and easy to understand. It is critical for C++ developers like me to realize that the value is an object separate from the labelc
.c
IS a memory location that contains a pointer to another memory location that is called1
, but in C++,c
IS a memory location that contains data that is1
.
– Ryan
1 hour ago
add a comment |Â
up vote
4
down vote
c
doesn't "Point at a
or b
"... it points at the 1
or 2
objects.
>>> a = 1
>>> b = 2
>>> c = a
>>> c
1
>>> c = b
>>> c
2
>>> b = 3
>>> c
2
This can be proven somewhat with id()
- b
and c
point at the same "thing":
>>> b = 2
>>> c = b
>>> id(b)
42766656
>>> id(c)
42766656
Your answer is a good balance between in depth and easy to understand. It is critical for C++ developers like me to realize that the value is an object separate from the labelc
.c
IS a memory location that contains a pointer to another memory location that is called1
, but in C++,c
IS a memory location that contains data that is1
.
– Ryan
1 hour ago
add a comment |Â
up vote
4
down vote
up vote
4
down vote
c
doesn't "Point at a
or b
"... it points at the 1
or 2
objects.
>>> a = 1
>>> b = 2
>>> c = a
>>> c
1
>>> c = b
>>> c
2
>>> b = 3
>>> c
2
This can be proven somewhat with id()
- b
and c
point at the same "thing":
>>> b = 2
>>> c = b
>>> id(b)
42766656
>>> id(c)
42766656
c
doesn't "Point at a
or b
"... it points at the 1
or 2
objects.
>>> a = 1
>>> b = 2
>>> c = a
>>> c
1
>>> c = b
>>> c
2
>>> b = 3
>>> c
2
This can be proven somewhat with id()
- b
and c
point at the same "thing":
>>> b = 2
>>> c = b
>>> id(b)
42766656
>>> id(c)
42766656
edited 2 hours ago
answered 2 hours ago
Attie
3,5241121
3,5241121
Your answer is a good balance between in depth and easy to understand. It is critical for C++ developers like me to realize that the value is an object separate from the labelc
.c
IS a memory location that contains a pointer to another memory location that is called1
, but in C++,c
IS a memory location that contains data that is1
.
– Ryan
1 hour ago
add a comment |Â
Your answer is a good balance between in depth and easy to understand. It is critical for C++ developers like me to realize that the value is an object separate from the labelc
.c
IS a memory location that contains a pointer to another memory location that is called1
, but in C++,c
IS a memory location that contains data that is1
.
– Ryan
1 hour ago
Your answer is a good balance between in depth and easy to understand. It is critical for C++ developers like me to realize that the value is an object separate from the label
c
. c
IS a memory location that contains a pointer to another memory location that is called 1
, but in C++, c
IS a memory location that contains data that is 1
.– Ryan
1 hour ago
Your answer is a good balance between in depth and easy to understand. It is critical for C++ developers like me to realize that the value is an object separate from the label
c
. c
IS a memory location that contains a pointer to another memory location that is called 1
, but in C++, c
IS a memory location that contains data that is 1
.– Ryan
1 hour ago
add a comment |Â
up vote
2
down vote
Answering both of your question at once What is the value of c? What does c point to?, I've added an step by step execution with the id()
of each variable with proper comment. Hope this helps you understand properly what is happening under the hood.
>>> a=1
>>> b=2
>>> print(id(a))
1574071312 # this is the address of a
>>> print(id(b))
1574071344 # this is the address of b
>>>c=a # assignment of a to c
>>> print(c)
1 # c will contain now the value of a
>>> print(id(c))
1574071312 # this is the address of c which is same as a
>>> c=b # re-assignment of b to c
>>> print(c)
2 # c wil contain now the value of b
>>> print(id(c))
1574071344 # this the address of c now which is same as b
add a comment |Â
up vote
2
down vote
Answering both of your question at once What is the value of c? What does c point to?, I've added an step by step execution with the id()
of each variable with proper comment. Hope this helps you understand properly what is happening under the hood.
>>> a=1
>>> b=2
>>> print(id(a))
1574071312 # this is the address of a
>>> print(id(b))
1574071344 # this is the address of b
>>>c=a # assignment of a to c
>>> print(c)
1 # c will contain now the value of a
>>> print(id(c))
1574071312 # this is the address of c which is same as a
>>> c=b # re-assignment of b to c
>>> print(c)
2 # c wil contain now the value of b
>>> print(id(c))
1574071344 # this the address of c now which is same as b
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Answering both of your question at once What is the value of c? What does c point to?, I've added an step by step execution with the id()
of each variable with proper comment. Hope this helps you understand properly what is happening under the hood.
>>> a=1
>>> b=2
>>> print(id(a))
1574071312 # this is the address of a
>>> print(id(b))
1574071344 # this is the address of b
>>>c=a # assignment of a to c
>>> print(c)
1 # c will contain now the value of a
>>> print(id(c))
1574071312 # this is the address of c which is same as a
>>> c=b # re-assignment of b to c
>>> print(c)
2 # c wil contain now the value of b
>>> print(id(c))
1574071344 # this the address of c now which is same as b
Answering both of your question at once What is the value of c? What does c point to?, I've added an step by step execution with the id()
of each variable with proper comment. Hope this helps you understand properly what is happening under the hood.
>>> a=1
>>> b=2
>>> print(id(a))
1574071312 # this is the address of a
>>> print(id(b))
1574071344 # this is the address of b
>>>c=a # assignment of a to c
>>> print(c)
1 # c will contain now the value of a
>>> print(id(c))
1574071312 # this is the address of c which is same as a
>>> c=b # re-assignment of b to c
>>> print(c)
2 # c wil contain now the value of b
>>> print(id(c))
1574071344 # this the address of c now which is same as b
answered 2 hours ago


don't angry me
11.8k22239
11.8k22239
add a comment |Â
add a comment |Â
up vote
1
down vote
Well my friend, in this example if c
is pointing to a
, it'll appear like they are pointing at the same value but not, for example if you're pointing
a = 2
c = a
then after this declaration, if you change the value of a = 3
, c
will change it's value to 3
also.
Imagine those variables values [2][3]
in boxes and the variables a, b, c
are just pointing to those boxes.
If one variable a
is pointing to one box, and the other variable c
is pointing to the variable a
that is pointing to the box [2]
, the last variable c
is just following the first variable a
, not the value [2]
, hope this explanation gets you happy.
New contributor
Joaquim Mucage is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Please edit your question, using formatting makes it easier readable
– Bert Verhees
2 hours ago
This is a good way to explain it to new programmers. The [ ] square brackets visualization is very helpful.
– Ryan
45 mins ago
add a comment |Â
up vote
1
down vote
Well my friend, in this example if c
is pointing to a
, it'll appear like they are pointing at the same value but not, for example if you're pointing
a = 2
c = a
then after this declaration, if you change the value of a = 3
, c
will change it's value to 3
also.
Imagine those variables values [2][3]
in boxes and the variables a, b, c
are just pointing to those boxes.
If one variable a
is pointing to one box, and the other variable c
is pointing to the variable a
that is pointing to the box [2]
, the last variable c
is just following the first variable a
, not the value [2]
, hope this explanation gets you happy.
New contributor
Joaquim Mucage is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Please edit your question, using formatting makes it easier readable
– Bert Verhees
2 hours ago
This is a good way to explain it to new programmers. The [ ] square brackets visualization is very helpful.
– Ryan
45 mins ago
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Well my friend, in this example if c
is pointing to a
, it'll appear like they are pointing at the same value but not, for example if you're pointing
a = 2
c = a
then after this declaration, if you change the value of a = 3
, c
will change it's value to 3
also.
Imagine those variables values [2][3]
in boxes and the variables a, b, c
are just pointing to those boxes.
If one variable a
is pointing to one box, and the other variable c
is pointing to the variable a
that is pointing to the box [2]
, the last variable c
is just following the first variable a
, not the value [2]
, hope this explanation gets you happy.
New contributor
Joaquim Mucage is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Well my friend, in this example if c
is pointing to a
, it'll appear like they are pointing at the same value but not, for example if you're pointing
a = 2
c = a
then after this declaration, if you change the value of a = 3
, c
will change it's value to 3
also.
Imagine those variables values [2][3]
in boxes and the variables a, b, c
are just pointing to those boxes.
If one variable a
is pointing to one box, and the other variable c
is pointing to the variable a
that is pointing to the box [2]
, the last variable c
is just following the first variable a
, not the value [2]
, hope this explanation gets you happy.
New contributor
Joaquim Mucage is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 47 mins ago
Ryan
777
777
New contributor
Joaquim Mucage is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered 2 hours ago


Joaquim Mucage
211
211
New contributor
Joaquim Mucage is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Joaquim Mucage is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Joaquim Mucage is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Please edit your question, using formatting makes it easier readable
– Bert Verhees
2 hours ago
This is a good way to explain it to new programmers. The [ ] square brackets visualization is very helpful.
– Ryan
45 mins ago
add a comment |Â
Please edit your question, using formatting makes it easier readable
– Bert Verhees
2 hours ago
This is a good way to explain it to new programmers. The [ ] square brackets visualization is very helpful.
– Ryan
45 mins ago
Please edit your question, using formatting makes it easier readable
– Bert Verhees
2 hours ago
Please edit your question, using formatting makes it easier readable
– Bert Verhees
2 hours ago
This is a good way to explain it to new programmers. The [ ] square brackets visualization is very helpful.
– Ryan
45 mins ago
This is a good way to explain it to new programmers. The [ ] square brackets visualization is very helpful.
– Ryan
45 mins ago
add a comment |Â
up vote
0
down vote
Basically, in the fourth line c variable is being overwritten by the value of b. As this is the last statement, c will hold the value of 2.
How do I explicitly force b's value to overwrite/copy to a's value?
– Ryan
2 hours ago
2
@Ryan in Python, you do not. Variables act like name-tags to objects, they are not memory addresses. Python doesn't have pointers.
– juanpa.arrivillaga
2 hours ago
This explanation is ambiguous, because it relies on me already understanding that the "c variable" is its own memory location pointing to another memory location.
– Ryan
1 hour ago
add a comment |Â
up vote
0
down vote
Basically, in the fourth line c variable is being overwritten by the value of b. As this is the last statement, c will hold the value of 2.
How do I explicitly force b's value to overwrite/copy to a's value?
– Ryan
2 hours ago
2
@Ryan in Python, you do not. Variables act like name-tags to objects, they are not memory addresses. Python doesn't have pointers.
– juanpa.arrivillaga
2 hours ago
This explanation is ambiguous, because it relies on me already understanding that the "c variable" is its own memory location pointing to another memory location.
– Ryan
1 hour ago
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Basically, in the fourth line c variable is being overwritten by the value of b. As this is the last statement, c will hold the value of 2.
Basically, in the fourth line c variable is being overwritten by the value of b. As this is the last statement, c will hold the value of 2.
answered 2 hours ago


Vittal Kamath
206
206
How do I explicitly force b's value to overwrite/copy to a's value?
– Ryan
2 hours ago
2
@Ryan in Python, you do not. Variables act like name-tags to objects, they are not memory addresses. Python doesn't have pointers.
– juanpa.arrivillaga
2 hours ago
This explanation is ambiguous, because it relies on me already understanding that the "c variable" is its own memory location pointing to another memory location.
– Ryan
1 hour ago
add a comment |Â
How do I explicitly force b's value to overwrite/copy to a's value?
– Ryan
2 hours ago
2
@Ryan in Python, you do not. Variables act like name-tags to objects, they are not memory addresses. Python doesn't have pointers.
– juanpa.arrivillaga
2 hours ago
This explanation is ambiguous, because it relies on me already understanding that the "c variable" is its own memory location pointing to another memory location.
– Ryan
1 hour ago
How do I explicitly force b's value to overwrite/copy to a's value?
– Ryan
2 hours ago
How do I explicitly force b's value to overwrite/copy to a's value?
– Ryan
2 hours ago
2
2
@Ryan in Python, you do not. Variables act like name-tags to objects, they are not memory addresses. Python doesn't have pointers.
– juanpa.arrivillaga
2 hours ago
@Ryan in Python, you do not. Variables act like name-tags to objects, they are not memory addresses. Python doesn't have pointers.
– juanpa.arrivillaga
2 hours ago
This explanation is ambiguous, because it relies on me already understanding that the "c variable" is its own memory location pointing to another memory location.
– Ryan
1 hour ago
This explanation is ambiguous, because it relies on me already understanding that the "c variable" is its own memory location pointing to another memory location.
– Ryan
1 hour ago
add a comment |Â
up vote
0
down vote
So to summarize a few of the really good answers I saw from others,
- "values" are the variables, themselves, and are objects without a name.
- Names of variables are "labels" that are their own separate objects, and they can point to any value objects.
- The Assignment operator points a label object to a value object.
Let's inaccurately go step by step through the assignment operation from the point of view of the Python interpreter:
First, we create a value.
[value obj]
Note: [ ] denotes a physical memory location. This means the value has its own unique memory address.
Next, we create a label.
[Label obj] -> nothing
Last, we assign the label to its value.
[Label obj] -> [value obj]
So,
a = 1
is the same as
[memorylocation containing "a"] -> [memorylocation containing 1]
and
c = b
is same as
[memorylocation containing "c"] -> "b" resolved to [memorylocation containing 2]
1
I think "variable object" and "name object" is just confusing. In Python, a namespace is quite literally adict
.
– juanpa.arrivillaga
24 mins ago
add a comment |Â
up vote
0
down vote
So to summarize a few of the really good answers I saw from others,
- "values" are the variables, themselves, and are objects without a name.
- Names of variables are "labels" that are their own separate objects, and they can point to any value objects.
- The Assignment operator points a label object to a value object.
Let's inaccurately go step by step through the assignment operation from the point of view of the Python interpreter:
First, we create a value.
[value obj]
Note: [ ] denotes a physical memory location. This means the value has its own unique memory address.
Next, we create a label.
[Label obj] -> nothing
Last, we assign the label to its value.
[Label obj] -> [value obj]
So,
a = 1
is the same as
[memorylocation containing "a"] -> [memorylocation containing 1]
and
c = b
is same as
[memorylocation containing "c"] -> "b" resolved to [memorylocation containing 2]
1
I think "variable object" and "name object" is just confusing. In Python, a namespace is quite literally adict
.
– juanpa.arrivillaga
24 mins ago
add a comment |Â
up vote
0
down vote
up vote
0
down vote
So to summarize a few of the really good answers I saw from others,
- "values" are the variables, themselves, and are objects without a name.
- Names of variables are "labels" that are their own separate objects, and they can point to any value objects.
- The Assignment operator points a label object to a value object.
Let's inaccurately go step by step through the assignment operation from the point of view of the Python interpreter:
First, we create a value.
[value obj]
Note: [ ] denotes a physical memory location. This means the value has its own unique memory address.
Next, we create a label.
[Label obj] -> nothing
Last, we assign the label to its value.
[Label obj] -> [value obj]
So,
a = 1
is the same as
[memorylocation containing "a"] -> [memorylocation containing 1]
and
c = b
is same as
[memorylocation containing "c"] -> "b" resolved to [memorylocation containing 2]
So to summarize a few of the really good answers I saw from others,
- "values" are the variables, themselves, and are objects without a name.
- Names of variables are "labels" that are their own separate objects, and they can point to any value objects.
- The Assignment operator points a label object to a value object.
Let's inaccurately go step by step through the assignment operation from the point of view of the Python interpreter:
First, we create a value.
[value obj]
Note: [ ] denotes a physical memory location. This means the value has its own unique memory address.
Next, we create a label.
[Label obj] -> nothing
Last, we assign the label to its value.
[Label obj] -> [value obj]
So,
a = 1
is the same as
[memorylocation containing "a"] -> [memorylocation containing 1]
and
c = b
is same as
[memorylocation containing "c"] -> "b" resolved to [memorylocation containing 2]
edited 15 mins ago
answered 51 mins ago
Ryan
777
777
1
I think "variable object" and "name object" is just confusing. In Python, a namespace is quite literally adict
.
– juanpa.arrivillaga
24 mins ago
add a comment |Â
1
I think "variable object" and "name object" is just confusing. In Python, a namespace is quite literally adict
.
– juanpa.arrivillaga
24 mins ago
1
1
I think "variable object" and "name object" is just confusing. In Python, a namespace is quite literally a
dict
.– juanpa.arrivillaga
24 mins ago
I think "variable object" and "name object" is just confusing. In Python, a namespace is quite literally a
dict
.– juanpa.arrivillaga
24 mins ago
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52685928%2fin-python-if-i-type-a-1-b-2-c-a-c-b-what-is-the-value-of-c-what-does-c-point%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
4
In python you can reassign variables as you wish. c now refers to the same objects as b refers. Try this
print(id(c) == id(b))
andprint(id(c) == id(a))
. Any similarity between c and a is now erased. See more about name here: docs.python.org/3/reference/executionmodel.html. I'm sure however there are more elaborate answers here on SO on the topic.– Anton vBR
2 hours ago
2
Ok after seeing your update: name point at objects. they don't hold any values. no copying is involved.
– Anton vBR
2 hours ago
2
Read the following article by StackOverflow legend, Ned Batchelder, that explains this exhaustively: Facts and myths about Python names and values
– juanpa.arrivillaga
2 hours ago
@juanpa.arrivillaga good article. Even has a code runtime visualizer.
– Ryan
2 hours ago
@juanpa.arrivillaga Exactly the reference we needed. Any1 who has interest in understanding this question should be linked to that page.
– Anton vBR
2 hours ago