I am trying to get the children list in text format from my Node class. I need this to be able to print the structure in JSON format.
When I am trying to call the getchildren()
method or directly use the same code in __str__
, in both cases I get a max recursion error. It comes with 'Above line was executed 196 times'.
I got some solutions saying increase this limit, that may solve my problem and I will try that if nothing else works, but am not able to get why am I getting this error.
It is something like I am trying to create a JSON of something that was supposed to be an XML so its quite nested and the source is a CSV file, the code shows a single Node of data.
Am pretty new to Python and stuff like this works well in Java so confused.
I have this code:
class Node:
_attributeDictionary = {}
_lst = []
def __init__(self, tag):
self._tag = tag
def setAttribute(self, attributeName, attributeValue):
self._attributeDictionary [attributeName]= attributeValue
def setChild(self, node):
self._lst.append(node)
def getchildren(self):
s = "["
for l in self._lst:
s = s + str(l) + "],"
return s[:-1]
def __str__(self):
s = "["
for l in self._lst:
s = s + str(l) + "],"
return "'" + self._tag + "':" + str(self._attributeDictionary) + "," + s[:-1] + ""
nodeA=Node("A")
nodeA.setAttribute("attA","valA")
nodeB=Node("B")
nodeB.setAttribute("attA","valB")
nodeA.setChild(nodeB)
print(str(nodeA))
Error:
s = s + str(l) + "],"
[Previous line repeated 196 more times]
RecursionError: maximum recursion depth exceeded