Rectangle 27 31

How to reproduce this error in python:

Python methods will return NoneType if you expect a tuple from them and fail to return anything to fill them up:

>>> def baz():
...   print("k")
... 
>>> a, b = baz()
k
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not iterable

You can also get that error if you assign NoneType to a variable:

>>> a = NoneType
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'NoneType' is not defined

If you try to iterate NoneType in a for loop, you get that error:

>>> for i in NoneType:
...   print("Yeah")
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'NoneType' is not defined

Try to concatenate None and a string, you get that error:

>>> bar = "something"
>>> foo = None
>>> print foo + bar
TypeError: cannot concatenate 'str' and 'NoneType' objects

If you use a variable passed in a method that contains NoneType, you get that error:

>>> def foo(data):
...   print(data)
... 
>>> foo(NoneType)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'NoneType' is not defined

The python interpreter converted your above code to pyc bytecode and then the Python virtual machine's line of execution encountered your for loop which invoked the __iter__ method on the variable called data.

data has the value None, which clearly has no __iter__ method, so the Python virtual machine is telling you what it sees: that NoneType object you fed it does not have a __iter__ method which hands me back an iteratable.

This is why Python's duck-typing is considered bad. You do something completely reasonable, and a completely reasonable exception comes along and the python virtual machine pukes up a bunch of unrelated nonsense all over the carpet.

Java doesn't have these problems because such a program wouldn't even compile since you haven't defined your return types and haven't specified what to do during an exception.

NoneType
None
NameError: name 'NoneType' is not defined
TypeError: cannot concatenate 'str' and 'NoneType' objects
TypeError: 'NoneType' object is not iterable

TypeError: 'NoneType' object is not iterable in Python - Stack Overflo...

python nonetype
Rectangle 27 108

Correct, But the common scenario author intended here is totally to skip the for loop instead of raising an exception. Python's design is flawed here. When None is treated as an iterable it must return empty list at least. This exception never helped anyone in real life other than making us insert few ugly if data is not None: kind of handling.

TypeError: 'NoneType' object is not iterable in Python - Stack Overflo...

python nonetype
Rectangle 27 8

It means that the data variable is passing None (which is type NoneType), its equivalent for nothing. So it can't be iterable as a list, as you are trying to do.

it would be nice if just iterated like an empty list... would nake for cleaner code and less error checking

@deltanine It would make for a lot of problems being more difficult to detect I think. I'm glad None is different from an empty iterable. If you want your described behaviour, just use for row in data or []:

TypeError: 'NoneType' object is not iterable in Python - Stack Overflo...

python nonetype
Rectangle 27 47

for row in data:
TypeError: 'NoneType' object is not iterable

Which object is it complaining about? Choice of two, row and data. In for row in data, which needs to be iterable? Only data.

What's the problem with data? Its type is NoneType. Only None has type NoneType. So data is None.

You can verify this in an IDE, or by inserting e.g. print "data is", repr(data) before the for statement, and re-running.

Think about what you need to do next: How should "no data" be represented? Do we write an empty file? Do we raise an exception or log a warning or keep silent?

TypeError: 'NoneType' object is not iterable in Python - Stack Overflo...

python nonetype
Rectangle 27 6

write_file(foo, bar)

But you haven't defined 'foo' correctly, or you have a typo in your code so that it's creating a new empty variable and passing it in.

TypeError: 'NoneType' object is not iterable in Python - Stack Overflo...

python nonetype
Rectangle 27 3

Another thing that can produce this error is when you are setting something equal to the return from a function, but forgot to actually return anything.

def foo(dict_of_dicts):
    for key, row in dict_of_dicts.items():
        for key, inner_row in row.items():
            Do SomeThing
    #Whoops, forgot to return all my stuff

return1, return2, return3 = foo(dict_of_dicts)

This is a little bit of a tough error to spot because the error can also be produced if the row variable happens to be None on one of the iterations. The way to spot it is that the trace fails on the last line and not inside the function.

If your only returning one variable from a function, I am not sure if the error would be produced... I suspect error "'NoneType' object is not iterable in Python" in this case is actually implying "Hey, I'm trying to iterate over the return values to assign them to these three variables in order but I'm only getting None to iterate over"

TypeError: 'NoneType' object is not iterable in Python - Stack Overflo...

python nonetype
Rectangle 27 473

try:
    iterator = iter(theElement)
except TypeError:
    # not iterable
else:
    # iterable

# for obj in iterator:
#     pass
import collections

if isinstance(theElement, collections.Iterable):
    # iterable
else:
    # not iterable
collections.Iterable
__getitem__
iter

Is iter() guaranteed to never throw a TypeError for any other reason??

@mehaase: The docs are not completely unambiguous, but I'm fairly sure that's the case.

@mehaase, don't listen to Georg. iter(x) invokes x.__iter__() and will propagate any exception it throws to the caller. See [example]( melpon.org/wandbox/permlink/J39itbseL2ZsZ1J9). P.S. Just in case someone reading this doesn't know yet: if x.__iter__() can't be found iter() falls back to using x.__getitem__(). see docs.

From "Fluent Python" by Luciano Ramalho: As of Python 3.4, the most accurate way to check whether an object x is iterable is to call iter(x) and handle a TypeError exception if it isnt. This is more accurate than using isinstance(x, abc.Iterable), because iter(x) also considers the legacy getitem method, while the Iterable ABC does not.

In Python, how do I determine if an object is iterable? - Stack Overfl...

python iterable
Rectangle 27 470

try:
    iterator = iter(theElement)
except TypeError:
    # not iterable
else:
    # iterable

# for obj in iterator:
#     pass
import collections

if isinstance(theElement, collections.Iterable):
    # iterable
else:
    # not iterable
collections.Iterable
__getitem__
iter

Is iter() guaranteed to never throw a TypeError for any other reason??

@mehaase: The docs are not completely unambiguous, but I'm fairly sure that's the case.

@mehaase, don't listen to Georg. iter(x) invokes x.__iter__() and will propagate any exception it throws to the caller. See [example]( melpon.org/wandbox/permlink/J39itbseL2ZsZ1J9). P.S. Just in case someone reading this doesn't know yet: if x.__iter__() can't be found iter() falls back to using x.__getitem__(). see docs.

From "Fluent Python" by Luciano Ramalho: As of Python 3.4, the most accurate way to check whether an object x is iterable is to call iter(x) and handle a TypeError exception if it isnt. This is more accurate than using isinstance(x, abc.Iterable), because iter(x) also considers the legacy getitem method, while the Iterable ABC does not.

In Python, how do I determine if an object is iterable? - Stack Overfl...

python iterable
Rectangle 27 2

return
self.choose_enemy_spell()
return self.choose_enemy_spell()

Here is the debugging thought process:

Thanks very much for your response! Seems like such a simple error but its really cleared up the process in mind! In my head I was with that line of code moving back to the start of the method and expecting it to do so until it reached the else statement Now I'm thinking actually it beginning the method, moving into the elf statement beginning the method again and reaching the else statement and returning that to the elif statement which was then not returning anything to the main body of code which was expected! Thanks!

python - Can't solve 'NoneType' object is not iterable TypeError - Sta...

python nonetype
Rectangle 27 17

try:
  #treat object as iterable
except TypeError, e:
  #object is not actually iterable

Don't run checks to see if your duck really is a duck to see if it is iterable or not, treat it as if it was and complain if it wasn't.

Technically, during iteration your computation might throw a TypeError and throw you off here, but basically yes.

I know in .NET it was a bad idea to have exceptions handle program flow, as exceptions were slow. How quickly does python handle exceptions?

@willem: Please use timeit to perform a benchmark. Python exceptions are often faster than if-statements. They can take a slightly shorter path through the interpreter.

A working try: statement is really fast. So if you have few exceptions, try-except is fast. If you expect many exceptions, if can be faster.

In Python, how do I determine if an object is iterable? - Stack Overfl...

python iterable
Rectangle 27 17

try:
  #treat object as iterable
except TypeError, e:
  #object is not actually iterable

Don't run checks to see if your duck really is a duck to see if it is iterable or not, treat it as if it was and complain if it wasn't.

Technically, during iteration your computation might throw a TypeError and throw you off here, but basically yes.

I know in .NET it was a bad idea to have exceptions handle program flow, as exceptions were slow. How quickly does python handle exceptions?

@willem: Please use timeit to perform a benchmark. Python exceptions are often faster than if-statements. They can take a slightly shorter path through the interpreter.

A working try: statement is really fast. So if you have few exceptions, try-except is fast. If you expect many exceptions, if can be faster.

In Python, how do I determine if an object is iterable? - Stack Overfl...

python iterable
Rectangle 27 22

It is not only an iterable, it is an iterator, which is why it can only traverse the file once. You may reset the file cursor with .seek(0) as many have suggested but you should, in most cases, only iterate a file once.

+1 for a nice, concise description of the iterable-vs.-iterator thing (which most of the other answers didn't even attempt, even though it's the key to the question).

Is file object in python an iterable - Stack Overflow

python iterable
Rectangle 27 1

The error you experience (TypeError: 'Request' object is not iterable) happened because a Request instance is being put into a field of the item (in the updated get_meta method function), while the feed exporter cannot serialize it.

You would need to return the get meta request to Scrapy, together with a meta argument to pass the half-parsed item. Here's an example of the updated parse method and a new parse_get_meta method:

def parse(self, response):
    for sel in response.xpath("a long string"):
        mainloader = ProductLoader(selector=sel)
        mainloader.add_value('category', 'Category Name')
        #mainloader.add_value('meta', self.get_meta(sel))
        # more data
        item = mainloader.load_item()
        get_meta_req = self.get_meta(sel)
        get_meta_req['meta']['item'] = item
        yield get_meta_req.replace(callback=self.parse_get_meta)

def parse_get_meta(self, response):
    """Parses a get meta response"""
    item = response.meta['item']
    # Parse the response and load the data here, e.g. item['foo'] = bar
    pass
    # Finally return the item
    return item

The two methods parse_get_meta and get_product_page were overlapping, so it was confusing to adapt your approach in my code, the engine was actually sending requests but without retrieving anything. The output is slightly different with all the key aligned as I also had to modifiy the Item class inside the items.py. Did I mention it works.

python - Scrapy: TypeError: 'Request' object is not iterable - Stack O...

python python-2.7 scrapy scrapy-spider
Rectangle 27 473

try:
    iterator = iter(theElement)
except TypeError:
    # not iterable
else:
    # iterable

# for obj in iterator:
#     pass
import collections

if isinstance(theElement, collections.Iterable):
    # iterable
else:
    # not iterable
collections.Iterable
__getitem__
iter

Is iter() guaranteed to never throw a TypeError for any other reason??

@mehaase: The docs are not completely unambiguous, but I'm fairly sure that's the case.

@mehaase, don't listen to Georg. iter(x) invokes x.__iter__() and will propagate any exception it throws to the caller. See [example]( melpon.org/wandbox/permlink/J39itbseL2ZsZ1J9). P.S. Just in case someone reading this doesn't know yet: if x.__iter__() can't be found iter() falls back to using x.__getitem__(). see docs.

From "Fluent Python" by Luciano Ramalho: As of Python 3.4, the most accurate way to check whether an object x is iterable is to call iter(x) and handle a TypeError exception if it isnt. This is more accurate than using isinstance(x, abc.Iterable), because iter(x) also considers the legacy getitem method, while the Iterable ABC does not.

In Python, how do I determine if an object is iterable? - Stack Overfl...

python iterable
Rectangle 27 6

all sequence types (such as list, str, and tuple) and some non-sequence types like dict and file and objects of any classes you define with an __iter__() or __getitem__() method. Iterables can be used in a for loop and in many other places where a sequence is needed (zip(), map(), ...). When an iterable object is passed as an argument to the built-in function iter(), it returns an iterator for the object.

Of course, given the general coding style for Python based on the fact that it's Easier to ask for forgiveness than permission., the general expectation is to use

try:
    for i in object_in_question:
        do_something
except TypeError:
    do_something_for_non_iterable

But if you need to check it explicitly, you can test for an iterable by hasattr(object_in_question, "__iter__") or hasattr(object_in_question, "__getitem__"). You need to check for both, because strs don't have an __iter__ method (at least not in Python 2, in Python 3 they do) and because generator objects don't have a __getitem__ method.

In Python, how do I determine if an object is iterable? - Stack Overfl...

python iterable
Rectangle 27 13

In Python <= 2.5, you can't and shouldn't - iterable was an "informal" interface.

But since Python 2.6 and 3.0 you can leverage the new ABC (abstract base class) infrastructure along with some builtin ABCs which are available in the collections module:

Now, whether this is desirable or actually works, is just a matter of conventions. As you can see, you can register a non-iterable object as Iterable - and it will raise an exception at runtime. Hence, isinstance acquires a "new" meaning - it just checks for "declared" type compatibility, which is a good way to go in Python.

On the other hand, if your object does not satisfy the interface you need, what are you going to do? Take the following example:

from collections import Iterable
from traceback import print_exc

def check_and_raise(x):
    if not isinstance(x, Iterable):
        raise TypeError, "%s is not iterable" % x
    else:
        for i in x:
            print i

def just_iter(x):
    for i in x:
        print i


class NotIterable(object):
    pass

if __name__ == "__main__":
    try:
        check_and_raise(5)
    except:
        print_exc()
        print

    try:
        just_iter(5)
    except:
        print_exc()
        print

    try:
        Iterable.register(NotIterable)
        ni = NotIterable()
        check_and_raise(ni)
    except:
        print_exc()
        print

If the object doesn't satisfy what you expect, you just throw a TypeError, but if the proper ABC has been registered, your check is unuseful. On the contrary, if the __iter__ method is available Python will automatically recognize object of that class as being Iterable.

So, if you just expect an iterable, iterate over it and forget it. On the other hand, if you need to do different things depending on input type, you might find the ABC infrastructure pretty useful.

don't use bare except: in the example code for beginners. It promotes bad practice.

J.F.S: I wouldn't, but I needed to go through multiple exception-raising code and I didn't want to catch the specific exception... I think the purpose of this code is pretty clear.

In Python, how do I determine if an object is iterable? - Stack Overfl...

python iterable
Rectangle 27 13

In Python <= 2.5, you can't and shouldn't - iterable was an "informal" interface.

But since Python 2.6 and 3.0 you can leverage the new ABC (abstract base class) infrastructure along with some builtin ABCs which are available in the collections module:

Now, whether this is desirable or actually works, is just a matter of conventions. As you can see, you can register a non-iterable object as Iterable - and it will raise an exception at runtime. Hence, isinstance acquires a "new" meaning - it just checks for "declared" type compatibility, which is a good way to go in Python.

On the other hand, if your object does not satisfy the interface you need, what are you going to do? Take the following example:

from collections import Iterable
from traceback import print_exc

def check_and_raise(x):
    if not isinstance(x, Iterable):
        raise TypeError, "%s is not iterable" % x
    else:
        for i in x:
            print i

def just_iter(x):
    for i in x:
        print i


class NotIterable(object):
    pass

if __name__ == "__main__":
    try:
        check_and_raise(5)
    except:
        print_exc()
        print

    try:
        just_iter(5)
    except:
        print_exc()
        print

    try:
        Iterable.register(NotIterable)
        ni = NotIterable()
        check_and_raise(ni)
    except:
        print_exc()
        print

If the object doesn't satisfy what you expect, you just throw a TypeError, but if the proper ABC has been registered, your check is unuseful. On the contrary, if the __iter__ method is available Python will automatically recognize object of that class as being Iterable.

So, if you just expect an iterable, iterate over it and forget it. On the other hand, if you need to do different things depending on input type, you might find the ABC infrastructure pretty useful.

don't use bare except: in the example code for beginners. It promotes bad practice.

J.F.S: I wouldn't, but I needed to go through multiple exception-raising code and I didn't want to catch the specific exception... I think the purpose of this code is pretty clear.

In Python, how do I determine if an object is iterable? - Stack Overfl...

python iterable
Rectangle 27 5

all sequence types (such as list, str, and tuple) and some non-sequence types like dict and file and objects of any classes you define with an __iter__() or __getitem__() method. Iterables can be used in a for loop and in many other places where a sequence is needed (zip(), map(), ...). When an iterable object is passed as an argument to the built-in function iter(), it returns an iterator for the object.

Of course, given the general coding style for Python based on the fact that it's Easier to ask for forgiveness than permission., the general expectation is to use

try:
    for i in object_in_question:
        do_something
except TypeError:
    do_something_for_non_iterable

But if you need to check it explicitly, you can test for an iterable by hasattr(object_in_question, "__iter__") or hasattr(object_in_question, "__getitem__"). You need to check for both, because strs don't have an __iter__ method (at least not in Python 2, in Python 3 they do) and because generator objects don't have a __getitem__ method.

In Python, how do I determine if an object is iterable? - Stack Overfl...

python iterable
Rectangle 27 6

newtotals = ("Client %d" % i,) + tuple(total)

total is a number. You can't convert it to a tuple like that; the tuple() class expects you to pass it a sequence. Try just building the tuple you want directly:

newtotals = ("Client %d" % i, total)

To add to kindall's answer, I think what OP meant by that line was newtotals = ("Client %d" % i,) + (total,)

@kindall That's great, thank you! Also, I just noticed that this prints from Client 1 to Client 10, is there a way to change it so that it prints from Client 0 to Client 9 so that it matches newitems?

I'd suggest using the enumerate function with your for loop. That way you don't need the separate statements to deal with i. So for i, each_item in enumerate(newitems).

python - Creating list of tuples ( TypeError: 'int' object is not iter...

python python-2.7
Rectangle 27 576

Checking for __iter__ works on sequence types, but it would fail on e.g. strings in Python 2. I would like to know the right answer too, until then, here is one possibility (which would work on strings, too):

try:
    some_object_iterator = iter(some_object)
except TypeError, te:
    print some_object, 'is not iterable'

The iter built-in checks for the __iter__ method or in the case of strings the __getitem__ method.

Another general pythonic approach is to assume an iterable, then fail gracefully if it does not work on the given object. The Python glossary:

Pythonic programming style that determines an object's type by inspection of its method or attribute signature rather than by explicit relationship to some type object ("If it looks like a duck and quacks like a duck, it must be a duck.") By emphasizing interfaces rather than specific types, well-designed code improves its flexibility by allowing polymorphic substitution. Duck-typing avoids tests using type() or isinstance(). Instead, it typically employs the EAFP (Easier to Ask Forgiveness than Permission) style of programming.

try:
   _ = (e for e in my_object)
except TypeError:
   print my_object, 'is not iterable'
collections
import collections

if isinstance(e, collections.Iterable):
    # e is iterable

However, this does not check for classes that are iterable through __getitem__.

[e for e in my_object] can raise an exception for other reasons, ie my_object is undefined or possible bugs in my_object implementation.

isinstance('', Sequence) == True
isinstance('', Iterable)
hasattr('', '__iter__') == False

If my_object is very large (say, infinite like itertools.count()) your list comprehension will take up a lot of time/memory. Better to make a generator, which will never try to build a (potentially infinite) list.

What if some_object throws TypeError caused by other reason(bugs etc.) too? How can we tell it from the "Not iterable TypeError"?

hasattr(u"hello", '__iter__')
True

In Python, how do I determine if an object is iterable? - Stack Overfl...

python iterable