Home > Blockchain >  How does nodes list get built up in __repr__(self) method while invoking other methods in class Link
How does nodes list get built up in __repr__(self) method while invoking other methods in class Link

Time:01-01

I'm trying to understand how the __repr__ method gets called and the nodes list gets updated during invocation of class's other methods. Sorry if my terminology isn't correct. I've only included the prepend and append methods for brevity. It looks to me like the list "nodes" would get initialized over and over again. Thanks!

class Node:
    
    """A node in a singly-linked list"""
    
    def __init__(self, dataval=None, nextval=None):
        self.dataval = dataval
        self.nextval = nextval
        
    def __repr__(self):
        return repr(self.dataval)
class LinkedList:
    def __init__(self):
        
        """Creating a new singly-linked list."""
        
        self.head = None
        
    def __repr__(self):
        
        """Creating a string representation of the data field in a list"""
        
        nodes = []
        curr = self.head
        
        while curr:
            nodes.append(repr(curr))
            curr = curr.nextval
            
        return '['   '->'.join(nodes)   ']'
    
    def prepend(self, dataval):
        
        """Insert a new element at the beginning of the list."""
        
        self.head = Node(dataval=dataval, nextval=self.head)
        
    def append(self, dataval):
        
        """Insert a new element at the end of the list."""
        
        if not self.head:
            self.head = Node(dataval=dataval)
            return
        
        curr = self.head
        
        while curr.nextval:
            curr = curr.nextval
            
        curr.nextval = Node(dataval=dataval)

The code works, I just don't understand quite how it works. It's from an online course about data structures.

CodePudding user response:

Inside the LinkedList's __repr__ you create a new local nodes list, you call the __repr__ of all of the nodes by passing on their nextval attribute and add their string representation to the list, and then concatenate the list together using '->'.join().

You can visualize it using '.join(nodes) + ']' def prepend(self, dataval): """Insert a new element at the beginning of the list.""" self.head = Node(dataval=dataval, nextval=self.head) def append(self, dataval): """Insert a new element at the end of the list.""" if not self.head: self.head = Node(dataval=dataval) return curr = self.head while curr.nextval: curr = curr.nextval curr.nextval = Node(dataval=dataval) l = LinkedList() l.append(1) l.append(2) l.append(3) print(repr(l))&cumulative=false&curInstr=40&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=[]&textReferences=false" rel="nofollow noreferrer">python tutor starting from step 41.

CodePudding user response:

You seem to be confused about the call to append in __repr__: that is not a call to the append method in your class. It is calling append on a native list type. The two methods happen to have the same name, and I guess that's where the mixup comes from.

The __repr__ method creates a list (not a LinkedList) and populates it with the string representations of the nodes in the linked list. Then it joins those strings to one string and returns it. During this process no other method of the LinkedList class is called.

CodePudding user response:

__repr__ isn't called in the prepend and append routines. The nodes list in LinkedList.__rep__ is only there temporarily to hold the nodes while its return value is built. In fact, your classes don't use list to hold nodes at all. LinkedList has a reference to the first Node in head and each Node has a reference to the next node in nextval.

  • Related