Question: Why does the following code print this ( < _ _ main _ _ . List 1 3 5 object at 0 x 0 0

Why does the following code print this (<__main__.List135 object at 0x000001D240A3CD30>,1)? I want the function firstN to return the first n elements of a list135 object (xs) as a new list135 object and output [1,2] in this example
class List135:
# create new list node. Defaults to a node representing the empty list
def __init__(self, initdata=None, initnext=None):
self._data = initdata
self._next = initnext
# is the list beginning with self the end of the list?
def empty(self):
return self._next == None
# return the first element of the list that starts with self
def first(self):
return self._data
# return the list that begins after the list that starts with self
def rest(self):
return self._next
# return a list beginning with data, followed by list beginning with self
def add(self, data):
return List135(data, self)
# tail-recursively accumulates ","+ str(cur._data) for each remaining item
def _str(self, cur, acc):
if cur._next == None:
return acc
else:
nextacc = acc +","+ str(cur._data)
nextcur = cur._next
return self._str(nextcur, nextacc)
def __str__(self):
if self._next == None:
return "[]"
else:
acc = str(self._data) # init accumulator with first item
cur = self._next # continue accumulation with next
return "["+ self._str(cur, acc)+"]"
def firstN(xs, n):
if n ==0:
return temp
else:
temp = List135()
temp.add(xs.first())
return (xs.rest(), n-1)
if __name__=='__main__':
xs = List135().add(3).add(2).add(1)
zs = firstN(xs,2)
print(str(zs))

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!