Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Python, if I type a=1 b=2 c=a c=b, what is the value of c? What does c point to?

Tags:

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
like image 218
Ryan Avatar asked Oct 07 '18 06:10

Ryan


People also ask

What is the type of 1/2 in Python?

type(1/2) will return type float. Python-3. x division operator follows the True Division.

What is the value of a in Python?

It is a numeric value given to different characters and symbols, for computers to store and manipulate. For example, the ASCII value of the letter 'A' is 65.

What does 1 :] mean in Python?

By artturijalli. In Python, [::-1] means reversing a string, list, or any iterable with an ordering. For example: hello = "Hello world"

What does == mean in Python?

The == operator compares the value or equality of two objects, whereas the Python is operator checks whether two variables point to the same object in memory. In the vast majority of cases, this means you should use the equality operators == and !=


1 Answers

There are no pointers to variables in Python. In particular, when you say this:

Is the statement replacing the pointer c -> a with pointer c -> 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.

like image 99
David Z Avatar answered Oct 04 '22 04:10

David Z