Variables

Even this section is called variables. Actually, in python they should be called names or tags.

  • In python everything is object.

  • We give a name to an object:

a = [1, 2, 3]
  • One object could have multiple names. When you assign a name to a new name, the object get a new name(reference).

b = a
  • Lists, dicts and sets are called mutable objects. With mutable object, you could assign its name to a new name(create a reference of object). AND you could change object itself(change stuff inside object, e.g. update a element in list). This change could be observed by all names.

>>> a[0] = -1
>>> print(a)
>>> print(b)
[-1, 2, 3]
[-1, 2, 3]
  • Ints, strings, tuples etc are called immutable objects. With immutable object, you could ONLY assign its name to a new name(reference). There isn’t anyway to manipulate object itself.

>>> t = (1,2,3)
>>> x = t
>>> print(t)
>>> print(x)
(1, 2, 3)
(1, 2, 3)

Understanding Python variables and Memory Management

Have you ever noticed any difference between variables in Python and C? For example, when you do an assignment like the following in C, it actually creates a block of memory space so that it can hold the value for that variable

In C++

int a = 1;

You can think of it as putting the value assigned in a box with the variable name as shown below.

_images/a1box.png

And for all the variables you create a new box is created with the variable name to hold the value. If you change the value of the variable the box will be updated with the new value. That means doing

a = 2;

will result in

_images/a2box.png

Assigning one variable to another makes a copy of the value and put that value in the new box.

int b = a;
_images/b2box.png _images/a2box.png

In Python

Variables work more like tags unlike the boxes you have seen before. When you do an assignment in Python, it tags the value with the variable name.

a = 1
_images/a1tag.png

and if you change the value of the varaible, it just changes the tag to the new value in memory. You dont need to do the housekeeping job of freeing the memory here. Python’s Automatic Garbage Collection does it for you. When a value is without names/tags it is automatically removed from memory.

a = 2
_images/a2tag.png

Assigning one variable to another makes a new tag bound to the same value as show below.

b = a
_images/ab2tag.png

Other languages have ‘variables’. Python has ‘names’.