2

I'm getting

TypeError: 'NoneType' object is not iterable 

on this line:

temp, function = findNext(function) 

and have no idea why this is failing. I am using function in while loops:

while 0 < len(function):
    …

but am not iterating through it. All of the returns in findNext(function) are pretty much

return 'somestring',function[1:]

and cannot understand why it thinks I'm iterating on one of those objects.

1
  • 1
    It was kind of hard to make sense of your question because you gave such small snips of code. Please see stackoverflow.com/help/how-to-ask Commented Sep 10, 2013 at 22:02

2 Answers 2

1

I am guessing that findNext falls off the end without returning anything, which makes it automatically return None. Kind of like this:

>>> def findNext(function):
...     if function == 'y':
...         return 'somestring',function[1:]
...
>>> function = 'x'
>>> print(findNext(function))
None
>>> temp, function = findNext(function)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not iterable

The solution would be to always return something.

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

1 Comment

Perfect! Thank you very much! I had forgotten to put a catch all return at the end!
0

The statement:

return 'somestring',function[1:]

is actually returning a tuple of length 2 and tuples are iterables. It would be more idiomatic to write that statement as:

return ('somestring', function[1:])

which makes its tuple nature far more obvious.

3 Comments

Okay, I had already changed that in my code, but how then can i get the tuple to combine with the variables it returns to? Do i have to say temp = tuple[0] \n function = tuple[1]?
I don't understand that question. You are doing "tuple unpacking" properly in the line temp, function = findNext(function) but I'm guessing that you are not unpacking consistently. It is hard to tell with the small bits of code you gave.
Sorry I am avoiding pasting the whole thing in, because it has a lot of repetitive if statements, but I can assure you that the unpacking is consistent. You got the question right, I was asking about unpacking. When it didn't work the first thing I looked for was consistency in the packing and unpacking. Thank you very much for the help.

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.