Why does attribute error shows for a none type attribute in a python class why it occurs?
Question:
I define a python linkedlist class but when I access the attribute it generate attribute error though i initialized the attribute in init. please help me . Here is the code . Thank you.
class LinkedList:
def __int__(self):
self.head = None
self.tail = None
def insert(self,data):
node = Node(data)
if self.head is None: # in this line it shows that no head attribute
self.head= node
self.tail = node
else:
self.tail.next = node
self.tail = node
class Node:
def __init__(self,data):
self.data = data
self.next = None
if __name__ == '__main__':
l = LinkedList()
ar = [1,2,5,6]
for a in ar:
l.insert(a)
print(l.head)
the self.head should worked but it doesn’t . why its happening .Anyone please help. Thank you.
Answers:
The issue in your code is a typo in the LinkedList class. Instead of int, you should use init as the constructor method name. The correct code should be as follows:
class LinkedList:
def __init__(self):
self.head = None
self.tail = None
def insert(self, data):
node = Node(data)
if self.head is None:
self.head = node
self.tail = node
else:
self.tail.next = node
self.tail = node
class Node:
def __init__(self, data):
self.data = data
self.next = None
if __name__ == '__main__':
l = LinkedList()
ar = [1, 2, 5, 6]
for a in ar:
l.insert(a)
print(l.head)
By changing int to init, you properly define the constructor method, which gets called when creating an instance of the class. This will correctly initialize the head and tail attributes, allowing you to access them without encountering an AttributeError.
I define a python linkedlist class but when I access the attribute it generate attribute error though i initialized the attribute in init. please help me . Here is the code . Thank you.
class LinkedList:
def __int__(self):
self.head = None
self.tail = None
def insert(self,data):
node = Node(data)
if self.head is None: # in this line it shows that no head attribute
self.head= node
self.tail = node
else:
self.tail.next = node
self.tail = node
class Node:
def __init__(self,data):
self.data = data
self.next = None
if __name__ == '__main__':
l = LinkedList()
ar = [1,2,5,6]
for a in ar:
l.insert(a)
print(l.head)
the self.head should worked but it doesn’t . why its happening .Anyone please help. Thank you.
The issue in your code is a typo in the LinkedList class. Instead of int, you should use init as the constructor method name. The correct code should be as follows:
class LinkedList:
def __init__(self):
self.head = None
self.tail = None
def insert(self, data):
node = Node(data)
if self.head is None:
self.head = node
self.tail = node
else:
self.tail.next = node
self.tail = node
class Node:
def __init__(self, data):
self.data = data
self.next = None
if __name__ == '__main__':
l = LinkedList()
ar = [1, 2, 5, 6]
for a in ar:
l.insert(a)
print(l.head)
By changing int to init, you properly define the constructor method, which gets called when creating an instance of the class. This will correctly initialize the head and tail attributes, allowing you to access them without encountering an AttributeError.