EzDevInfo.com

forbiddenfruit

Patch built-in python objects Forbiddenfruit by clarete

Make int iterable with forbiddenfruit [duplicate]

This question already has an answer here:

I know, this is wrong, but is it possible? I thought an object is considered an iterable when its .__iter__ method returned an iterator? So why doesn't this work?

>>> from forbiddenfruit import curse
>>> def __iter__(self):
...     for i in range(self):
...         yield i
>>> curse(int, "__iter__", __iter__)
>>> for x in 5:
...     print x
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable

int does seem to have an __iter__ method now:

>>> int(5).__iter__
<bound method int.__iter__ of 5>

Source: (StackOverflow)