Variables and Objects on Python

In most programming languages, including C++ and Java, we must declare each variable, specifying its type, before it can be used. This is called static typing, because the compiler knows at compile time what type each variable is. Python, like most very high level languages, uses a different approach: Vari- ables have no type restrictions (dynamic typing), and they don’t need to be declared.

We could learn about Python’s variables and identifiers by creating and executing a file, as we did with hello.py in the preceding section. But for trying out small code snippets we don’t need to create a file at all. We can just enter the lines directly in the IDLE Python Shell window at the >>> prompt:

>>> x =  71
>>> y  = "Dove"

The whitespace around the assignment operator = is optional but is included because it makes the code easier to read. As a matter of style we will always put one space before and after binary operators. On the other hand, it is important that each statement occupies its own line and has no extraneous leading whitespace. This is because Python uses indentation and line breaks to signify its block structure, rather than the braces and semicolons used by many other programming languages.

Now we are ready to review what the two lines actually do. The first line creates an object of type int and binds the name x to it.★ The second line creates an object of type str (an 8-bit string type) and binds the name y to it.

Some Python programmers refer to names (such as the x and y used earlier), as object references since they refer to objects rather than being objects in their own right. For basic data types like int and str it makes no difference whether we see their variables as “objects” or as “object references”; they behave in the same way as they do in other programming languages:

>>> x =  82
>>> x  +=  7  
>>> x
89

Later on we will see cases where the fact that Python variables are object references makes a difference.

Python has two ways of comparing objects: by “identity” and by “value”. An object’s identity is effectively its address in memory, and this is what an object reference holds. If we use comparison operators, such as == and <, we get value comparison. For example, two strings are equal using == if they both contain the same text. If we use is we get identity comparison, which is fast because we are just comparing two addresses and don’t have to look at the objects themselves. An object’s identity can be obtained by calling id() on an object reference. Python has a special object called None. This can be assigned to any variable and it means that the variable has no value. There is only ever one instance of the None object, so we can always use the fast is and is not comparisons when testing for it. Notice that we wrote x on its own at the >>> prompt. If we write an expression or variable in IDLE, its value is automatically printed. In a program, we must use an explicit print statement to print an expression. For example:

print  x

Python’s print statement is an operator, not a function, and for this reason it is invoked without using parentheses (just as we use + and other operators without them).

Earlier we said that Python uses dynamic typing. There are two factors involved in this. First, we can assign any object to any variable; for example, we could write:

x  =  47
x  = "Heron"

After the first line x’s type is int, and after the second line x’s type is str, so clearly the type associated with the name x is determined by what the name is bound to, and not by any intrinsic property of its own. For this reason , we do not need to associate a particular type with a particular name.

The second aspect of Python’s dynamic typing is that the typing is strong: Python does not permit operations between incompatible types, as the following example, typed into IDLE, shows:

>>> x =  41
>>> y  = "Flamingo"
>>> x  +  y 

Traceback (most recent  call last):
File  <pyshell#2>, line  1,  in  <module>
x  +  y  
TypeError:   unsupported operand type(s)  for  +:  'int' and  'str' 

When we attempted to apply the binary + operator, Python raised a TypeError exception and refused to perform the operation.

If we were to assign to y a type compatible with x’s type, such as an int or float, the addition would work fine:

>>> x =  41
>>> y  = 8.5
>>> x  +  y  
49.5

Although x and y are of different types (int and float), Python provides the same kind of automatic type promotion that other languages use, so the x is converted to a float and the calculation performed is actually 41.0 + 8.5.


Object references and objects

Assigning a value to a variable is called binding, since we bind names to objects. If we assign a new object to an existing variable, we are said to be rebinding the name. This is illustrated in Figure 1.2. When we do this, what happens to the object the name was originally bound to? For example:

>>> x  = "Sparrow"
>>> x  = 9.8

What has happened to the str object that holds the text “Sparrow”? Once an object has no names bound to it, it is scheduled for garbage collection, and in due course it may be deleted from memory. This is very similar to how things work in Java.

Python variable names consist of ASCII letters, digits, and underscores (_). Variable names should begin with a letter, and they are case-sensitive (rowan, Rowan, and roWan are three different variables). No Python variable should be given the name of any of Python’s keywords (see Table 1.1), nor of Python’s built-in constants such as None, True, or False.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: