I am trying to implement a recursive function within a class declaration in Python. However, the function doesn't seem to accept parameters. If I declare a recursive function outside the Class, it works.
[A while loop][1] will also do the trick. (See "Traversing values).
I've banged my head on the keyboard enough to permit an SO post, IMHO.
class Node:
def __init__(self, value):
self.data = value
self.next = None
def traverse(self, node):
print(node.data)
if (node.next == None):
return
else:
node.traverse(node.next)
>>> a = Node('a')
>>> b = Node('b')
>>> c = Node('c')
>>> a.next = b
>>> b.next = c
>>> Node.traverse(a)
Traceback (most recent call last):
File "<pyshell#62>", line 1, in <module>
Node.traverse(a)
TypeError: traverse() missing 1 required positional argument: 'node'
[1]: https://medium.com/#kojinoshiba/data-structures-in-python-series-1-linked-lists-d9f848537b4d
A more typical implementation of this would be.
Node Code
class Node:
def __init__(self, value):
self.data = value
self.next = None
def traverse(self): # instance method (no 2nd argument node)
if self: # check if node
print(self.data) # output data
if self.next: # check for next node
self.next.traverse() # recursive call to next node if any
Test
a = Node('a')
b = Node('b')
c = Node('c')
a.next = b
b.next = c
a.traverse() # Note: Node.traverse(a) also works
# This is because
# When you call an instance method (e.g. traverse) from an
# instance object (e.g. a), Python automatically passes
# that instance object as the first argument (in addition
# to any other arguments) to the function call
# Thus: a.traverse() becomes Node.traverse(a)
Output
a
b
c
You need to make traverse a class method. Currently it says you're missing the node argument because in the line Node.traverse(a) you provided self=a and not node=a.
class Node:
def __init__(self, value):
self.data = value
self.next = None
#classmethod # This is the only addition needed :)
def traverse(self, node):
print(node.data)
if node.next is None:
return
else:
node.traverse(node.next)
In Python Cookbook, page 117, you will find this recipe which is a better solution for this problem:
class Node:
def __init__(self, value):
self._value = value
self._children = []
def __repr__(self):
return 'Node({!r})'.format(self._value)
def add_child(self, node):
self._children.append(node)
def __iter__(self):
return iter(self._children)
def depth_first(self):
yield self
for c in self:
yield from c.depth_first()
if __name__ == '__main__':
root = Node(0)
child1 = Node(1)
child2 = Node(2)
root.add_child(child1)
root.add_child(child2)
child1.add_child(Node(3))
child1.add_child(Node(4))
child2.add_child(Node(5))
for ch in root.depth_first():
print(ch)
Try to use this.
Related
I am using two python files, one file in which a class of linked list present and another file is the one in which I am importing first file so that I can use linked list I built in first file. The second file is for reverse file. I have already done reverse using iteration part, now trying to build a code for reverse using recursion and for that I am calling and passing arguments inside function but something did not work out and it is showing TypeError like this function has no arguments.
Please check it out my code followed by error
Second file
from code.linkedlist import *
llist=linkedlist()
llist.appendnodesatbegin(23)
llist.appendnodesatbegin(45)
llist.appendnodesatbegin(67)
llist.appendnodesatbegin(12)
llist.appendnodesatbegin(-11)
llist.appendnodesatbegin(0)
print ("Before reverse")
llist.display()
def reverseiterative():
llist.current = llist.head
llist.prev = None
while (llist.current):
llist.next = llist.current.next
llist.current.next = llist.prev
llist.prev = llist.current
llist.current = llist.next
llist.head = llist.prev
reverseiterative()
print("After the reverse of list using iterative method")
llist.display()
llist.p=llist.head
llist.prev=None
def reverserecursive(p,prev):
next1=llist.p.next
p.next=prev
if llist.next1 is None:
return
else:
reverserecursive(next1,p)
reverserecursive(llist.p,llist.prev)
print("After the reverse of list using recursive method")
llist.display()
first file:
class node:
def __init__(self,data):
self.data=data
self.next=None
class linkedlist:
def __init__(self):
self.head=None
self.last_pointer=None
def appendnodesatbegin(self,data):
newnode=node(data)
if(self.head==None):
self.head=newnode
self.last_pointer=newnode
else:
self.last_pointer.next=newnode
self.last_pointer=self.last_pointer.next
def appendnodesatend(self,data):
newnode=node(data)
newnode.next=self.head
self.head=newnode
def appendatmid(self,prev,new):
temp=self.head
newnode=node(new)
while(temp):
if(temp.data==prev):
newnode.next=temp.next
temp.next=newnode
temp=temp.next
def display(self):
temp=self.head
while(temp):
print(temp.data)
temp=temp.next
#def reversedisplay(self):
error is
reverseiterative(llist.p,llist.prev)
TypeError: reverseiterative() takes no arguments (2 given)
reverseiterative as defined:
def reverseiterative():
takes no argument, you are calling it with 2.
You were probably supposed to call reverserecursive given the arguments you passed and the argument's in the function signature:
def reverserecursive(p,prev):
Your function doesn't take any parameters in it's deceleration:
reverseiterative(foo, bar):
This (or whatever values you wish to process) will fix it.
from code.linkedlist import *
llist=linkedlist()
llist.appendnodesatbegin(23)
llist.appendnodesatbegin(45)
llist.appendnodesatbegin(67)
llist.appendnodesatbegin(12)
llist.appendnodesatbegin(-11)
llist.appendnodesatbegin(0)
print ("Before reverse")
llist.display()
def reverseiterative(self):
self.current = self.head
self.prev = None
while (self.current):
self.next = self.current.next
self.current.next = self.prev
self.prev = self.current
self.current = self.next
self.head = self.prev
llist.reverseiterative=reverseiterative
llist.reverseiterative()
print("After the reverse of list using iterative method")
llist.display()
def reverserecursive(self,p,prev):
next=p.next
p.next=prev
if next is None:
return
else:
self.reverserecursive(next,p)
llist.p=llist.head
llist.prev=None
llist.reverserecursive(llist.p,llist.prev)
print("After the reverse of list using recursive method")
llist.display()
Here is the second part to fix your issue:
class node:
def __init__(self,data):
self.data=data
self.next=None
class linkedlist:
def __init__(self):
self.head=None
self.last_pointer=None
def appendnodesatbegin(self,data):
newnode=node(data)
if(self.head==None):
self.head=newnode
self.last_pointer=newnode
else:
self.last_pointer.next=newnode
self.last_pointer=self.last_pointer.next
def appendnodesatend(self,data):
newnode=node(data)
newnode.next=self.head
self.head=newnode
def appendatmid(self,prev,new):
I tried to write a code that protects the pointer of a linked list. The setter should point only to an instance which belongs to the same class. Usually isinstance() or type() work after the class is defined. But the setter is a method inside that class, hence the class is not fully defined yet.
I have no doubt that type() gives out error. But I wonder why isinstance(instance, class) yields False if calling from another class.
#define the Node class
class Node:
"""Class Node contain only data and a next pointer variables."""
def __init__(self, Data = None):
self.data = Data
self.__next = None
def getNext(self):
"""The getter"""
return self.__next
def setNext(self, NextNode):
"""The setter"""
#if possible check if NewNode is an instance of Node before pointing to it.
#I have tried "isinstance(), type(), etc. but failed.
if isinstance(NextNode, Node):
self.__next = NextNode
else:
print('The Next pointer should point to "Node" only.')
Then check if isinstance is working
ANode = Node((1,2,3))
BNode = Node((5,6))
ANode.setNext(BNode)
print(BNode)
print(ANode.getNext())
Both prints yield the same address
<__main__.Node object at 0x112162828>
<__main__.Node object at 0x112162828>
So everything looks fine. But When I call from the LinkedList class, printed below, the isinstance yields False, as seen from my warning.
class LinkedList:
"""This class is the Linked List of Node."""
def __init__(self, FirstNode = None):
"""Initialize by creating an empty List. __First hold the pointer that point to the first node."""
if FirstNode is None:
self.__first = Node(None)
self.__last = self.__first
elif type(FirstNode) is Node:
self.__first = FirstNode
self.__last = self.__first
else:
print('To create a linked-list enter nothing or a Node.')
raise TypeError
def getFirst(self):
return self.__first
def append(self, NewLastNode):
"""Add LastNode to the end of the list."""
if not isinstance(NewLastNode,Node):
raise TypeError
OldLast = self.__last
OldLast.setNext(NewLastNode)
self.__last = NewLastNode
NewLastNode.setNext(None)
def removeFirstNode(self):
"""Remove the first node (when the buffer is full)."""
OldFirst = self.__first
NewFirst = OldFirst.getNext()
if NewFirst == None:
# just clear data
OldFirst.data = None
else:
self.__first = NewFirst
del OldFirst
Then I create an instance of the LinkedList class
LL = LinkedList(Node((1,2)))
NewNode = Node((2.0, 3.0, -10))
Surely isinstance works fine here
isinstance(NewNode,Node)
yields True, but
LL.append(NewNode)
which will call Node.setNext() and there the isinstance() yields False as the else in Node.setNext() prints out
The Next pointer should point to "Node" only.
The piece of code that's giving you the error is this:
NewLastNode.setNext(None)
because you're trying to set the next element to an object that it's not a Node instance, hence the error.
I think you could simply remove this statement, as your self.__last is now correctly pointing to your NewLastNode. So your code will become:
def append(self, NewLastNode):
"""Add LastNode to the end of the list."""
if not isinstance(NewLastNode,Node):
raise TypeError
OldLast = self.__last
OldLast.setNext(NewLastNode)
self.__last = NewLastNode
Inside a recursive class function, how do i use a class member as default argument?
I am currently using following snippet that works (python 3). height is recursive class function. Default argument that i want to use for node is self._root. In order to achieve this, i do something like following but thats a very round about way where i have to define another function height_tree. Is there a more direct approach?
# following code works #
class BTree:
def __init__(self, node=None):
self._root = node
def height_tree(self):
return self.height(self._root)
def height(self, node):
if node is not None:
height = max(self.height(node.get_left()), self.height(node.get_right())) + 1
else:
height = 0
return height
I want to do something like following but it obviously doesn't works !
def height(self, node=self._root)
# code from above follows
I however cannot use alternate trick of assigning root to node when node is None since that is one of the end condition in recursion
def height(self, node)
if node is None:
node = self._root
# this will conflict with logic in code block above#
Are there any recommended ways to handle such a situation?
You can use a sentinel object:
sentinel = object()
def height(self, node=sentinel)
if node is sentinel:
node = self._root
...
Since such object would have a unique memory address, it would never be identical to any other object (including None).
You can always create arbitrary sentinels:
SENTINEL = object()
class BTree:
def __init__(self, node=None):
self._root = node
def height_tree(self):
return self.height(self._root)
def height(self, node=SENTINEL):
if node is SENTINEL:
node = self._root
elif node is not None:
height = max(self.height(node.get_left()), self.height(node.get_right())) + 1
else:
height = 0
return height
I'm writing an implementation of doubly linked lists. In order to traverse the list, I'm using something like:
class Node:
""" A node in our linked list """
def __init__(self, value: Any, next: Union['Node', None] =None,
previous: Union['Node', None] =None) -> None:
self.value = value
self.next = next
self.previous = previous
...
def __next__(self, direction: int =1) -> Union['Node', None]:
if direction == 1:
return self.get_next()
else:
return self.get_previous()
...
where get_next and get_previous are just getters of self.next and self.previous.
However, PyCharm yells at me for trying to call next as
next(some_node, direction=-1). What's the proper way to do this?
Besides __iter__ there is also __reversed__. Both are required to return iterators. The __next__ method should be implemented on iterators (not on node-classes). Note that all magic methods (when called by a function like next instead of directly invoked) need to implement the expected arguments not more - not less.
For example a doubly linked list could just implement __iter__ and __reversed__ and rely on next and previous attribute of the Node:
class Node(object):
def __init__(self, val, nxt, prv):
self.val = val
self.nxt = nxt
self.prv = prv
class DoublyLinkedList(object):
def __init__(self, base=None, last=None):
self.base = base
self.last = last
def prepend(self, val):
new = Node(val, self.base, None)
if self.base is None:
self.base = new
self.last = new
else:
self.base.prv = new
self.base = new
def append(self, val):
new = Node(val, None, self.last)
if self.last is None:
self.base = new
self.last = new
else:
self.last.nxt = new
self.last = new
def __iter__(self):
current = self.base
while current is not None:
yield current
current = current.nxt
def __reversed__(self):
current = self.last
while current is not None:
yield current
current = current.prv
For example:
dl = DoublyLinkedList()
dl.prepend(10)
dl.prepend(20)
dl.prepend(30)
for i in dl:
print(i.val)
gives:
30
20
10
similar for reversed:
for i in reversed(dl):
print(i.val)
# prints:
10
20
30
__next__ is part of the iterator protocol and should be used as described in said protocol, doing otherwise only make problems with the rest python.
In your case just rename the function to simple next and use as some_node.next(-1), though I would change the direction argument to a boolean, as that is how you use it, and its name too. Like this for example
class None:
...
def next(self, forward:bool=True) -> Union['Node', None]:
if forward:
return self.get_next()
else:
return self.get_previous()
and use as some_node.next(), some_node.next(False) or even some_node.next(0) (using 0 instead of False for the same effect)
The extra argument to next is a default value, and __next__ doesn't take any extra arguments. Python doesn't have any sort of two-way iterators. If your interface is not exactly the same as for i in obj:, then you should write your own.
I have created a descriptor for lists.
After testing it seems that every time I append a value to a list of one instance, it is being added to another instance as well.
Even weirder, in the unittests it keeps appending to the list, and not resetting on every test.
My descriptor main class:
class Field(object):
def __init__(self, type_, name, value=None, required=False):
self.type = type_
self.name = "_" + name
self.required = required
self._value = value
def __get__(self, instance, owner):
return getattr(instance, self.name, self.value)
def __set__(self, instance, value):
raise NotImplementedError
def __delete__(self, instance):
raise AttributeError("Can't delete attribute")
#property
def value(self):
return self._value
#value.setter
def value(self, value):
self._value = value if value else self.type()
Descriptor list class:
class ListField(Field):
def __init__(self, name, value_type):
super(ListField, self).__init__(list, name, value=[])
self.value_type = value_type
def __set__(self, instance, value):
if not isinstance(value, list):
raise TypeError("{} must be a list".format(self.name))
setattr(instance, self.name, value)
def __iter__(self):
for item in self.value:
yield item
def __len__(self):
return len(self.value)
def __getitem__(self, item):
return self.value[item]
def append(self, value):
if not isinstance(value, self.value_type):
raise TypeError("Value is list {} must be of type {}".format(self.name, self.value_type))
self.value.append(value)
Unittests:
# Class I created solely for testing purposes
class ListTestClass(object):
l = ListField("l", int)
class TestListFieldClass(unittest.TestCase):
def setUp(self):
self.listobject = ListTestClass()
def test_add(self):
# The first number is added to the list
self.listobject.l.append(2)
def test_multiple_instances(self):
# This test works just fine
l1 = ListField("l1", int)
l2 = ListField("l2", int)
l1.append(1)
l2.append(2)
self.assertEqual(l1[0], 1)
self.assertEqual(l2[0], 2)
def test_add_multiple(self):
# This test works just fine
l1 = ListField("l1", int)
l1.append(1)
l1.append(2)
self.assertEqual(l1[0], 1)
self.assertEqual(l1[1], 2)
def test_add_error(self):
# This test works just fine
with self.assertRaises(TypeError):
l1 = ListField("l1", int)
l1.append("1")
def test_overwrite_list(self):
# This test works just fine
l1 = ListField("l1", int)
l1 = []
l1.append(1)
def test_overwrite_error(self):
# This test works just fine
l1 = ListTestClass()
l1.l.append(1)
with self.assertRaises(TypeError):
l1.l = "foo"
def test_multiple_model_instances(self):
# I create 2 more instances of ListTestClass
l1 = ListTestClass()
l2 = ListTestClass()
l1.l.append(1)
l2.l.append(2)
self.assertEqual(l1.l[0], 1)
self.assertEqual(l2.l[0], 2)
The last test fails
Failure
Traceback (most recent call last):
File "/home/user/project/tests/test_fields.py", line 211, in test_multiple_model_instances
self.assertEqual(l1.l[0], 1)
AssertionError: 2 != 1
When I look at the values for l1.1 and l2.l, they both have a list containing [2, 1, 2]
What am I missing here?
I looked to the memory addresses and it seems that the lists all point to the same object.
class ListFieldTest(object):
lf1 = ListField("lf1", int)
class TestClass(object):
def __init__(self):
l1 = ListFieldTest()
l2 = ListFieldTest()
l1.lf1.append(1)
l2.lf1.append(2)
print(l1.lf1)
print(l2.lf1)
print(hex(id(l1)))
print(hex(id(l2)))
print(hex(id(l1.lf1)))
print(hex(id(l2.lf1)))
This prints
[1, 2]
[1, 2]
0x7f987da018d0 --> Address for l1
0x7f987da01910 --> Address for l2
0x7f987d9c4bd8 --> Address for l1.lf1
0x7f987d9c4bd8 --> Address for l2.lf1
ListTestClass.l is a class attribute, so it is shared by all instances of the class. Instead, you should create an instance attribute, eg in the __init__ method:
class ListTestClass(object):
def __init__(self):
self.l = ListField("l", int)
Similar remarks apply to ListFieldTest. There may be other similar problems elsewhere in your code, I haven't examined it closely.
According to this source, the proper form is
class ListTestClass(object):
l_attrib = ListField("l", int)
def __init__(self)
self.l = l_attrib
Thanks to both #PM 2Ring and volcano I found the answer.
In the end this works great for value types:
class IntTestClass(object):
i = IntegerField("i")
However for a reference type (like a list) that won't work and you have to add a new list
class ListTestClass(object):
l = ListField("l", int)
def __init__(self):
self.l = []