Data Structures and Algorithms using Python
Copyright © 2023 by Rance Necaise
Table Of Contents
Data Structures and Algorithms using Python
Copyright © 2023
by Rance Necaise

7.1 Objects and References

Before turning our attention to the topic of linked structures, it will be helpful to quickly review variable references and objects. Linked structures are built around the concept of objects and references to objects that are stored in variables.

References

In Python, a variable is a named location that stores a reference to an object. They do not store the data themselves. For example, the following statements

name = "John Smith"
idNum = 42
avg = 3.45

create objects and variables that can be illustrated as shown below

If we assign a new reference to an existing variable,

idNum = 70

it replaces the original reference

When all references to an object are removed, the object is marked as garbage and will eventually be destroyed. That is, the memory used by that object is returned to the program for reuse.

The None Reference

A variable can have a special value None to indicate that it does not reference or point to any object.

avg = None

This value is used to initialize a variable that should exist, but not store a reference to any particular object.

Aliases

Consider the following code segment,

pointA = Point(2, 8)
pointB = Point(15, 7)
pointC = Point(2, 8)

which creates three point objects using the Point class from Chapter 1

When two variables reference the same object,

target = pointC

they are aliases. Thus, the object is known by multiple names.

Sometimes it is important to know if a variable is an alias for a given object. When we use the == operator, we are testing for equality between two objects. That is, we are asking if the two objects contain the same value or represent the same entity. For example,

if target == pointA :
  print("These are the same points.")
else :
  print("They are not the same points.")

will print

They are the same points.

since the two points contain the same x- and y-coordinates and thus are the same point in the Cartesian coordinate system.

Question 7.1.1

What would be the result if we changed the condition part in the above code segment to

if target == pointB :

The code would print They are not the same points. since the two objects do not contain the same x- and y-coordinates. Remember, we implemented the __eq__ method to return True if the two objects contained the same x- and y-coordinates, and False otherwise.

Thus, we can not use the == operator to determine if two variables are aliases for the same object. You use the is operator instead

if target is pointA :
  print("The variables are aliases")
else :
  print("They are not aliases")

The is operator compares the references stored in the two variables to determine if they refer to the same object. It returns the appropriate Boolean value. In this case, the code will print

They are not aliases.

since each variable refers to a different object. While those objects represent the same point in space, they are not the same object.

Question 7.1.2

What would be the result if we changed the condition part in the above code segment to

if target is pointC :  # read this as "if target is an alias for pointC"

The code would print The variables are aliases both variables refer to the same object.

Python also provides the is not version of the operator, which does the exact opposite of the is operator. That is, it returns True if the two variables are not aliases, and False otherwise.

Question 7.1.3

What is the result of executing this code segment?

if target is not pointC :  # read this as "if target is not an alias for pointC"
  print("They are not aliases")
else :
  print("They are aliases")

It will print They aliases since both variables refer to the same object, but this time we are testing for the negative (that they are not aliases), which is false.

Page last modified on August 25, 2023, at 06:24 PM