2

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) if x.is_converted() is True: nset.add(x) if x.contacted is None: memotable[node.gen][node.genind] = nset else: nset.union(self.legacy(x, memotable)) memotable[node.gen][node.genind] = nset 

Full traceback as requested:

Traceback (most recent call last):

File "F:\Dropbox\CS\a4\skeleton\trialtest.py", line 142, in test_legacy_and_frac()

File "F:\Dropbox\CS\a4\skeleton\trialtest.py", line 125, in test_legacy_and_frac cunittest2.assert_equals(set([n10,n12,n21]), t.legacy(n00,mtable))

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

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

TypeError: 'NoneType' object is not iterable

8
  • 2
    Please post the Full Traceback.. It helps understand the problem. Commented Apr 2, 2013 at 18:45
  • 1
    Are you iterating over x.contacted before this? Showing more code helps. Commented Apr 2, 2013 at 18:45
  • 3
    Please reduce your program to the shortest complete program that still demonstrates the error. Then copy-paste that program into your question. See SSCCE.org for more info. Commented Apr 2, 2013 at 18:47
  • 1
    We need more code to answer this usefully. Nowhere in this code do you iterate over x, so we're left to assume you're either iterating elsewhere, or else your error has nothing to do with the variable you're checking. If you don't provide the code needed to reproduce (or at least understand) your error, it's much harder to fix. Commented Apr 2, 2013 at 18:47
  • Added the full code and traceback Commented Apr 2, 2013 at 18:52

3 Answers 3

3

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.

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

2 Comments

Sorry, I should've posted more code. x.contacted (which is a list), is iterated in the for statement I've added.
First, not it still isn't iterated in the for statement you added. Second, the traceback very clearly says that the exception is in legacy nset.union(self.legacy(x, memotable)).
0

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.

Comments

0

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 call last): File "<stdin>", line 1, in <module> TypeError: 'NoneType' object is not iterable 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.