Question

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.

Était-ce utile?

La solution

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.

Autres conseils

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top