if iter(iterator) is iterator
Chris Angelico
rosuav at gmail.com
Mon Nov 14 00:04:08 EST 2016
On Mon, Nov 14, 2016 at 3:54 PM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
>> Any particular reason to write it that way, rather than:
>>
>> def isiterable(obj):
>> try:
>> iter(obj)
>> return True
>> except TypeError:
>> return False
>
>
> class BadIterable:
> def __iter__(self):
> self.launch_missiles()
> return self
>
>
> In general, merely testing whether something is a certain kind of thing
> shouldn't actually run that thing's code.
True, but ISTR there being a possibility that __iter__ could raise to
indicate that this isn't actually iterable. Maybe I'm recalling wrong.
ChrisA
More information about the Python-list
mailing list