0

I am absolutely new to Wrapper Classes in Python. I was trying to implement it in a program of linked list where multiple linked list are in use. My code is:

def nodewrap(cls):
    class Nodewrap:
        def __init__(self):
            self.head = None
            self.tail = None
    return Nodewrap
    
@nodewrap
class Node:
    def __init__(self,data):
        self.data = data
        self.next = None

class Intersection:
    def addnode(self,d):
        newnode = Node(d)
        if head == None:
            head = tail = newnode
        else:
            tail.next = newnode
            tail = newnode

obj1 = Intersection()
obj2 = Intersection()
obj3 = Intersection()
s1 = int(input("Enter size of 1st list : \n"))
for i in range(s1):
    obj1.addnode(int(input("Enter the data : \n")))
s2 = int(input("Enter size of 1st list : \n"))
for i in range(s2):
    obj2.addnode(int(input("Enter the data : \n")))
temp1 = obj1.head
for i in range(s1):
    temp2 = obj2.head
    for j in range(s2):
        if temp1.data == temp2.data:
            obj3.addnode(temp1.data)
            break
        temp2 = temp2.next
    temp1 = temp1.next
print("Intersection is :")
temp = obj3.head
while temp!=None:
    print(temp.data,end=" ")
    temp = temp.next

I thought of using a wrapper class to wrap the class Node instead of using objects of the class Intersection only with data fields as head, tail. But it is giving me some sort of error with regards to init().

Please help.

I was trying to learn it from here: https://www.geeksforgeeks.org/wrapper-class-in-python/

  • I'm not sure why you want to wrap the Node class, as then every node would have a head and tail attribute (besides its data and next attributes), which is a waste of space. – trincot Mar 04 '22 at 10:31
  • @trincot I know it is useless but it's like I just wanted to try out with this new thing. Please help – Surya Majumder Mar 04 '22 at 14:32

1 Answers1

1

I think I understand what you want to do, but I think that you don't want to use a decorator, but you want to inherit from NodeWrap class

class Nodewrap:
    head = None
    tail = None
    
class Node(NodeWrap):
    def __init__(self,data):
        self.data = data
        self.next = None

But I don't see any reason why to inherit this way. This should be enough, for a linked list. I have added is_first and is_last property

from __future__ import annotations


class Node:
    prev_node = None
    next_node = None

    def __init__(self, data):
        self.data = data

    def add_node(self, node: Node) -> None:
        if self.prev_node is not None:
            raise ValueError('Previous node already defined.')
        self.next_node = node
        node.prev_node = self
    
    @property
    def is_first(self) -> bool:
        return self.prev_node is None

    @property
    def is_last(self) -> bool:
        return self.next_node is None

You can implement next, iter and create an Iterator class.

I don't recommend using next as the variable name. from __future__ import annotations reference here. It's just for self reference annotation.

Peter Trcka
  • 1,279
  • 1
  • 16
  • 21