Mohammed Fahad Kaleem - 1 year ago 71
Python Question

I am a beginner and I started learning python programming and I am stuck with an error. I get a type error

``````class Node:

def __init__(self):
self.data = None
self.next = Node

def setData(self,data)
self.data = data

def getData(self):
return self.data

def setNext(self,next):
self.next = next

def getNext(self):
return self.next

def hasNext(self):
return self.next!=None

def __init__(self):
self.length = 0

def listLength(self):
length = 0

while currentNode.hasNext:
length = length + 1
currentNode = currentNode.getNext()
return length

"""
Methods to Insert nodes in a Linked List:
# insertNode: Use this method to simply insert a node to the Linked List
# insertTail: Use this method to insert a node at the tail of the Linked List
# insertAtPosition: Use this method to insert a node at a particular position of the Linked List
"""

def insertNode(self,node):
if self.length == 0:
else:
self.insertTail(node)

nodeToBeInserted = Node()
nodeToBeInserted.setData(data)
if self.length == 0:
else:
self.length = self.length + 1

def insertTail(self,data):
nodeToBeInserted = Node()
nodeToBeInserted.setData(data)

while currentNode.getNext() != None:
currentNode = currentNode.getNext()
currentNode.setNext(nodeToBeInserted)
self.length = self.length + 1

def insertAtPosition(self,data, position):
if position > self.length or position < 0:
print("Invalid position!, The size of the Linked List is:%s"%self.length)
else:
if position ==0:
else:
nodeToBeInserted = Node()
nodeToBeInserted.setData(data)
count = 0
while count < position - 1:
currentNode = currentNode.getNext()
count = count + 1
nodeToBeInserted.setNext(currentNode.getNext())
currentNode.setNext(nodeToBeInserted)
self.length = self.length+1

ll.insertNode(1)
ll.insertNode(2)
ll.insertNode(3)
``````

The error I am seeing is:

``````Traceback (most recent call last):
ll.insertNode(2)
self.insertTail(node)
while currentNode.getNext() != None:
TypeError: getNext() missing 1 required positional argument: 'self'

Process finished with exit code 1
``````

If someone can please explain me the reason for this error it will be appreciated.

Thank you.

In `Node` class:

``````self.next = Node
``````

Why do you make this statement? Assign class itself to next is unreasonable. it should be:

``````self.next = None
``````

Another thing, I would like to suggest is you'd better design a good `__init__` function for each class. Think the `__init__` function in a class as a constructor for the class.

In your `__init__` function, no arguments are provided, which means when you instantiate the class, you cannot pass any data to it. In practice, we always need to provide some data to instantiate a class. Let's take the `Node` class for example, when we use it, we often already know the data this node should store, and the `next` node information, may or may not be known. So the better way to define its `__init__` function is:

``````def Node():
def __init__(self, data, next=None):
self.data = data
self.next = next
``````

Next time, you make new instance with `data` property known:

``````node = Node(data=1)
``````

If you also know the `next` property:

``````node = Node(data=1, next=next)
``````

This make you more convenient, without need to call `setX` function.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download