Linking Objects Using Pointer-like Variables in Python
Objects can be linked by variables, because variables are references to objects in Python. For example, consider the objects shown in Figure 1.
Figure 1
The objects have pointer-like attributes p and c. Attribute p points to the parent object and c points to the child object.
The class of the objects shown in Figure 1 is coded as the class Data in this way.
class Data:
count = 0
def __init__(self):
self.p = 0
self.c = 0
self.d = ""
self.id = Data.count
Data.count += 1
def print(self):
print("ID: {0} Data =
{1}".format(self.id, self.d))
When p or c is used to point to more than one objects, the types of p or c may be dictionaries, in which case the attribute is initialized, for example, in this way
self.p = {}
or
self.c = {}
Attribute d stores the data, and attribute id has the number assigned to the object. Attribute count of the class Data is used to set the number to the attribute id of the object. The method print displays the values of attributes id and d.
The sample program, which is shown at the end of this website, prepares two objects p_start and p_end, between which objects created are linked. They are initialized as follows
p_start = Data()
p_end = Data()
p_end.id = -1
p_start.c = p_end
p_end.p = p_start
p_current = p_start
Data.count = 1
Variable p_start indicates the start of the links of the objects, and p_end indicates the end of the links. Variable p_current is used to indicate the current object. The attribute count of the class Data is initialized to be 1.
String data to be stored in objects are read in by the following code.
while True:
s = input('Data = ')
if (s == ""):
break
else:
p_temp = Data()
p_temp.d = s
p_current.c = p_temp
p_temp.p = p_current
p_temp.c = p_end
p_current
= p_temp
p_end.p = p_current
When the empty string “” is read in, pressing the Enter-key immediately after the prompt ‘Data = ‘, the input-data stage ends. The data read in are displayed in the order of the last to the first by the following code.
if Data.count > 1:
while True:
p_current.print()
p_current = p_current.p
if
p_current.id == 0:
break
Note that attribute id of p_start is 0.
Then, the data are displayed in the order from the first to the last by the following code.
p_current = p_current.c
while p_current.id != -1:
p_current.print()
p_current = p_current.c
Note that attribute id of p_end is -1.
The whole code of the sample program is this.
============== A Sample Program ==========================
class Data:
count = 0
def __init__(self):
self.p = 0
self.c = 0
self.d = ""
self.id = Data.count
Data.count += 1
def print(self):
print("ID: {0} Data =
{1}".format(self.id, self.d))
p_start = Data()
p_end = Data()
p_end.id = -1
p_start.c = p_end
p_end.p = p_start
p_current = p_start
Data.count = 1
while True:
s = input('Data = ')
if (s == ""):
break
else:
p_temp = Data()
p_temp.d = s
p_current.c = p_temp
p_temp.p = p_current
p_temp.c = p_end
p_current = p_temp
p_end.p = p_current
if Data.count > 1:
while True:
p_current.print()
p_current = p_current.p
if
p_current.id == 0:
break
p_current = p_current.c
while p_current.id != -1:
p_current.print()
p_current = p_current.c
=========================================
An example of execution of the program is shown in Figure 2.
Figure 2