Home      |       Contents       |       About

Prev: Namespaces and Modules       |       Next: Where do types come from?

Variables and Dynamic Typing

(a) Variables

What exactly is then a variable in Python?

  • Variables do not exist in Python - at least not in the way they exist in other languages ('static type' languages as explained further below)
  • The property of variability in Python emerges while changing the namespace, that is, while changing the mapping (the binding, the object reference) of various names to various object-values during runtime.

Variabe names

  • For reasons of consistency with other programming environments and common understanding we retain the 'variable' keyword and talk about 'variable names', referring to the character sequences that are acceptable as Python names.
  • There are two simple rules for writing names:
    • (a) Acceptable names consist of: (underscore or letter) + (any number of letters, digits or underscores)
    • (b) Names are case sensitive: 'spam' is different from 'Spam' and different from 'SPAM'

Reserved words

  • ...that should not be used as names:

Assignment

  • Obviously assignment commands are of special importance in Python. The first time a name appears in an assignment command, Python integrates the name in the namespace and establishes a binding (setting a pointer) from the name to the referenced object.
  • Assignments can be written in ways that allow multiple bindings of names and objects. See the examples below:
In [9]:
# Assignment examples
a = b = c = 0       # all names are bound to value object '0' 

a, b = 1, 1.5       # a is bound to 1, b to 1.5 

a, b, c = 'foo'     # equivalent to: a='f', b='o', c='o'

a, b, c = (1,2,3)   # equivalent to: a=1, b=2, c=3

a, b, *c = 'spam'   # equivalent to: a='s', b='p', c=['a','m']

a, b = b, a         # swapping values 
    

(b) Dynamic typing

  • Python is a 'dynamically typed' language (or simply: 'dynamic' language) in contrast to other popular languages (C/C++, Java, etc.) which are 'statically typed' ('static') languages

What is 'dynamic typing'?

  • In Python (and in other dynamic languages) the type properties are not associated with the variable names but with the value object at the right place of an assignment.
  • During runtime any specific name can be bound to objects of different type (integers, string, float, lists, etc.) and, as long as this binding is valid, the variable name 'carries' the type properties of the object it is associated with. This dynamic change of name type without demanding any specific 'declaration' of the variable-name type, gives to Python its dynamic typing character.
  • See the example:
In [5]:
x = 'Hello world!'
print(type(x), len(x))

x = 1
print(type(x), len(x))
<class 'str'> 12
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-ea5fdaa6f2d1> in <module>()
      3 
      4 x = 1
----> 5 print(type(x), len(x))
      6 

TypeError: object of type 'int' has no len()
  • The first line binds name 'x' to the string object "Hello world!". Printing type(x) returns the name of the class that generates string type objects (class 'str'), while len(x) returns the length of the string in characters.
  • Things, however, change after the 'x=1' assignment. Now, type(x) returns 'int' (as 'x' is associated with the integer value object 1) and printing len(x) raises a 'TypeError' exception since integer objects do not have the len() property.
  • This is a clear example of the dynamic typing in Python code; no declaration required while the properties of the variable-names depend on their binding with objects that possess these properties.

Static typing...

... on the contrary requires that:

  • (a) The types of variable names are declared before any assignment.
  • (b) The values given to variables during runtime are of the type declared in the beginning of the program.

Thus, a program in C could start like this:

/* variable definition: */
int a, b;

/* assignment */
a = 10;
b = 20;

. Free learning material
. See full copyright and disclaimer notice