0

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)
9
  • You have a function called parse and you're modifying a variable called parse, which refers to your function. Can you post all of your code? Commented Jan 24, 2013 at 6:20
  • parse.index etc are some attributes of this very function parse, where are they assigned? You may want to consider a class instead. Commented Jan 24, 2013 at 6:20
  • @Blender I'd prefer not to since it's a 270 line monstrosity Commented Jan 24, 2013 at 6:24
  • @LukeMaresca: 270 lines is tiny. Commented Jan 24, 2013 at 6:24
  • 1
    @LukeMaresca: Just paste all of your code. There's no point in debugging code that we can't actually see. Commented Jan 24, 2013 at 6:30

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.