Skip to content Skip to sidebar Skip to footer

Python 'if X Is None' Not Catching Nonetype

The below snippet of code keeps returning a 'NoneType isn't iterable' error. Why doesn't the if statement catch this? inset = set() for x in node.contacted: print type(x) i

Solution 1:

The if statement guarantees that x.contacted isn't None.

But x.contacted isn't what you're trying to iterate or index, so it isn't guarding anything.

There's no reason memotable or memotable[node.gen] can't be None even though x.contacted is something else. For that matter, we have no idea of what the code inside self.legacy(x, memotable) does—maybe it tries to iterate x, or other_table[x], or who knows what, any of which could be None.

This is why you need to look at the entire traceback, not just the error string. It will tell you exactly which statement failed, and why.


And now that you've pasted the traceback:

File"F:\Dropbox\CS\a4\skeleton\trial.py", line 138, in legacy nset.union(self.legacy(x, memotable))

Yep, it's something that happens inside that self.legacy line, and it has absolutely nothing to do with x.contacted. The problem is almost certainly that your self.legacy method is returning None, so you're doing nset.union(None).

Again, whether x.contacted is or is not None is completely irrelevant here, so your check doesn't guard you here.

If you want us to debug the problem in that function, you will have to give us the code to that function, instead of code that has nothing to do with the error. Maybe it's something silly, like doing a + b instead of return a + b at the end, or maybe it's some deep logic error, but there's really no way we can guess.

Solution 2:

Check the value of memotable and memotable[node.gen] as it can not be said to guaranteed that they are not None if x.contacted is not None (without the code).

If you mention the values of the variables here and Post the Full Traceback, we may be able to point out the problem more precisely.

Solution 3:

The exception occurs because the function call self.legacy(x, memotable) returns None.

The traceback indicates the error occurs in nset.union(self.legacy(x, memotable)), and set.union() raises that exception when its argument is None. (I'm assuming nset is a set. Your code defines inset = set(), but does not show where nset comes from)

>>>set().union(None)
Traceback (most recent calllast):
  File "<stdin>", line 1, in<module>
TypeError: 'NoneType' object isnot iterable

Post a Comment for "Python 'if X Is None' Not Catching Nonetype"