This is my second day of learning python (I know the basics of C++ and some OOP.), and I have some slight confusion regarding variables in python.
Here is how I understand them currently:
Python variables are references (or pointers?) to objects (which are either mutable or immutable). When we have something like num = 5
, the immutable object 5
is created somewhere in memory, and the name-object reference pair num
is created in a certain namespace. When we have a = num
, nothing is being copied, but now both variables refer to the same object and a
is added to the same namespace.
This is where my book, Automate the boring stuff with Python, confuses me. As it's a newbie book, it doesn't mention objects, namespaces, etc., and it attempts to explain the following code:
>>> spam = 42 >>> cheese = spam >>> spam = 100 >>> spam 100 >>> cheese 42
The explanation it offers is exactly the same as that of a C++ book, which I am not happy about as we are dealing with references/pointers to objects. So in this case, I guess that in the 3rd line, as integers are immutable, spam
is being assigned an entirely new pointer/reference to a different location in memory, i.e. the memory that it was initially pointing to wasn't modified. Hence we have cheese
referring to the initial object referred to by spam
. Is this the correct explanation?
To summarize: Python lets you create variables simply by assigning a value to the variable, without the need to declare the variable upfront. The value assigned to a variable determines the variable type. Different types may support some operations which others don't.
After a value is assigned to a variable using the assignment operator, you can assign the value of that variable to another variable using the assignment operator. var myVar; myVar = 5; var myNum; myNum = myVar; The above declares a myVar variable with no value, then assigns it the value 5 .
The assignment operator, denoted by the “=” symbol, is the operator that is used to assign values to variables in Python. The line x=1 takes the known value, 1, and assigns that value to the variable with name “x”. After executing this line, this number will be stored into this variable.
You can assign the same value to multiple variables by using = consecutively. This is useful, for example, when initializing multiple variables to the same value. It is also possible to assign another value into one after assigning the same value.
As a C++ developer you can think of Python variables as pointers.
Thus when you write spam = 100
, this means that you "assign the pointer", which was previously pointing to the object 42
, to point to the object 100
.
Earlier on, cheese
was assigned to point to the same object as spam
pointed to, which happened to be 42
at that time. Since you have not modified cheese
, it still points to 42
.
Immutability has nothing to do with it in this case, since pointer assignment does not change anything about the object being pointed to.
It is correct you can more or less thing of variables as pointers. However example code would help greatly with explaining how this actually is working.
First, we will heavily utilize the id
function:
Return the “identity” of an object. This is an integer which is guaranteed to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same id() value.
It's likely this will return different absolute values on your machine.
Consider this example:
>>> foo = 'a string' >>> id(foo) 4565302640 >>> bar = 'a different string' >>> id(bar) 4565321816 >>> bar = foo >>> id(bar) == id(foo) True >>> id(bar) 4565302640
You can see that:
when we change the value of foo, it is assigned to a different id:
>>> foo = 42 >>> id(foo) 4561661488 >>> foo = 'oh no' >>> id(foo) 4565257832
An interesting observation too is that integers implicitly have this functionality up to 256:
>>> a = 100 >>> b = 100 >>> c = 100 >>> id(a) == id(b) == id(c) True
However beyond 256 this is no longer true:
>>> a = 256 >>> b = 256 >>> id(a) == id(b) True >>> a = 257 >>> b = 257 >>> id(a) == id(b) False
however assigning a
to b
will indeed keep the id the same as shown before:
>>> a = b >>> id(a) == id(b) True
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With