Menu
Home
Log in / Register
 
Home arrow Computer Science arrow Python Programming Fundamentals
< Prev   CONTENTS   Next >

1.11 The Reference Type and Assignment Statements

There is one type in Python that is typically not seen, but nevertheless is important to understand. It is called the reference type. A reference is a pointer that points to an object. A pointer is the address of an object. Each object in memory is stored at a unique address and a reference is a pointer that points to an object.

An assignment statement makes a reference point to an object. The general form of an assignment statement is:

< identifier > = < expression >

An identifier is any letters, digits, or underscores written without spaces between them. The identifier must begin with a letter or underscore. It cannot start with a digit. The expression is any expression that when evaluated results in one of the types described in Sect. 1.10. The left hand side of the equals sign must be an identifier and only one identifier. The right hand side of the equals sign can contain any expression that may be evaluated.

In Fig. 1.15, the variable R1_width (orange in the figure) is a reference that points at the integer object 10 colored green in the figure. This is what happens in memory in response to the assignment statement:

R1_width = 10

The 0x 264 is the reference value, written in hexadecimal, which is a pointer (i.e. the address) that points at the integer object 10. However, typically you don't see reference values in Python. Instead, you see what a reference points to. So if you type R1_width in the Python shell after executing the statement above, you won't see 0x 264 printed to the screen, you'll see 10, the value that R1_width refers to. When

Fig. 1.15 A reference

Fig. 1.16 Before

you set a breakpoint and look at the stack data in the debugger you will also see what the reference refers to, not the reference itself (see Fig. 1.13).

It is possible, and common, in Python to write statements like this:

x = 1

# do something with x

x = x + 1

According to what we have just seen, Fig. 1.16 depicts the state of memory after executing the first line of code and before executing the second line of code. In the second line of code, writing x=x+1 is not an algebraic statement. It is an assignment statement where one is added to the value that x refers to. The correct way to read an assignment statement is from right to left. The expression on the right hand side of the equals sign is evaluated to produce an object. The equals sign takes the reference to the new value and stores it in the reference named by the identifier on the left hand side of the equals sign. So, to properly understand how an assignment statement works, it must be read from right to left. After executing the second statement (the line beginning with a pound sign is a comment and is not executed), the state of memory looks like Fig. 1.17. The reference called x is updated to point to the new value that results from adding the old value referred to by x and the 1 together.

The space for the two left over objects containing the integers 1 in Fig. 1.17 is reclaimed by the garbage collector. You can think of the garbage collector as your favorite arcade game character running around memory looking for unattached objects (objects with no references pointing to them—the stuff in the cloud in Fig. 1.17). When such an object is found the garbage collector reclaims that memory for use later much like the video game character eats dots and fruit as it runs around.

Fig. 1.17 After

The garbage collector reclaims the space in memory occupied by unreferenced objects so the space can be used later. Not all programming languages include garbage collection but many languages developed recently include it and Python is one of these languages. This is a nice feature of a language because otherwise we would have to be responsible for freeing all of our own memory ourselves.

 
Found a mistake? Please highlight the word and press Shift + Enter  
< Prev   CONTENTS   Next >
 
Subjects
Accounting
Business & Finance
Communication
Computer Science
Economics
Education
Engineering
Environment
Geography
Health
History
Language & Literature
Law
Management
Marketing
Philosophy
Political science
Psychology
Religion
Sociology
Travel