In python, I'm trying to get a depth of recursion(dor). Before adding all the dor things into my code, it worked ~fine, but after adding the dor stuff, I received an attribute error concerning my code. Here is the function where I'm receiving the error
def parse(json,dor=0):
parse.index[dor]=0
parse.keyList[dor]=[]
parse.jsonDict[dor]=dict()
parse.json[dor]=remove_white(json)
Disclaimer: what you are doing is most likely The Wrong Thing To Do
Your code worked before because (I assume) you were setting an attribute on a function object:
def foo():
foo.bar = 4
When run, the function object sets an attribute bar
on itself. However, when you added the __setitem__
(with the square brackets):
def foo():
foo.bar[dor] = 4
You're now saying that you want to modify foo.bar
, but foo.bar
doesn't exist yet! You can "fix" this by setting up the object manually, before you run it for the first time:
def foo():
foo.bar[dor] = 4
foo.bar = {}
foo()
Most likely, you want to avoid this whole mess altogether by using a separate object to keep track of the recursion depth in your code. Just because you can do something doesn't mean you should.
EDIT: Looking at your code, it seems like you should be using a class instead of a function for parse
. Using a class makes sense because you're encapsulating mutable state with a set of methods that act on it. Of course, I'm also obligated to point you to the standard library JSON module.