tags:

views:

279

answers:

4

I've been looking for the NoneType for half a day. I've put 'print' and dir() all through the generation of the Object represented by t2. I've looked at the data structure after the crash using 'post mortem' and nowhere can I find a NoneType. I was wondering if perhaps it's one of those errors that are initiated by some other part of the code (wishful thinking) and I was wondering if anybody recognizes this? ( k2 is an 'int' )

 File "C:\Python26\Code\OO.py", line 48, in removeSubtreeFromTree
    assert getattr(parent, branch) is subtreenode
TypeError: getattr(): attribute name must be string, not 'NoneType

 File "C:\Python26\Code\OO.py", line 94, in theSwapper
    st2, p2, b2 = self.removeSubtreeFromTree(t2, k2)
TypeError: 'NoneType' object is not iterable
+2  A: 

for some reason, at the point of the assert line, the value of branch is None.

If your second exception is separate, Then most likely what is happening is the method call self.removeSubtreeFromTree() is returning None, instead of a sequence (like a tuple), so when Python tries to unpack it into the variables, it fails.

TokenMacGuy
You got it. That's what happened.
Peter Stewart
+5  A: 

NoneType is the type of the None object. So, in the first error, branch is None. The second error is tougher to diagnose without seeing the source code, but suggests that somewhere in t2, the data structure isn't exactly as you believe.

When this comes up for me, I usually find that I've forgotten to end one of my functions with a return statement. Functions without an explicit return will return None.

Managu
...or functions that have multiple exit points and not an explicit return for each one.
David Berger
With the help of all the responses, I found the 'NoneType is not iterable' error. I'd put a try: except: clause in and had indented all the way to the end of the function, so it returned None as you say. The other error shows up infrequently (3000 iterations on average,and I am using None as a value as John Fouhy noted, so I suspect one of those is sneaking by, Thanks for all the help!
Peter Stewart
+1  A: 

I agree with Managu that it's likely you've forgotten to return a value from a function. I do that all the time.

As another possibility, I presume you are writing some kind of tree data structure. Is it possible that you're using None to indicate "this node has no children" and you aren't handling that case correctly?

John Fouhy
A: 

Another one that got me was in-place functions, like list.append() (can't use that in a function call, list.append() returns None and changes the variable).

I spent the better part of a day and a half chasing that bug....

Jonathanb