Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #42396

Re: collections.Iterator __subclasshook__ does not check if next() is callable

Newsgroups comp.lang.python
Date 2013-03-31 12:44 -0700
References <a108caf6-538b-4ca0-978a-97bacc7ca7e8@googlegroups.com>
Message-ID <7c0fe05a-e28f-4e43-bb3b-5a3903c9cb6f@googlegroups.com> (permalink)
Subject Re: collections.Iterator __subclasshook__ does not check if next() is callable
From Byron Ruth <bjruth@gmail.com>

Show all headers | View raw


Raymond's replied to my follow-up and made me realize that the `next` property could return a callable and it would be transparent to the caller.

On Sunday, March 31, 2013 1:57:08 PM UTC-4, Byron Ruth wrote:
> I submitted this as bug last night: http://bugs.python.org/issue17584 and was *honored* to be rejected by Raymond Hettinger. However, I would like feedback on whether my concern (this bug) is justified and clarity if not.
> 
> 
> 
> Consider:
> 
> 
> 
> ```python
> 
> class A(object):
> 
>     def __init__(self):
> 
>         self.r = iter(range(5))
> 
>     def __iter__(self):
> 
>         return self
> 
>     @property
> 
>     def next(self):
> 
>         return next(self.r)
> 
> ```
> 
> 
> 
> The `next` method is a property, however:
> 
> 
> 
> ```python
> 
> from collections import Iterator
> 
> a = A()
> 
> isinstance(a, Iterator) # True
> 
> next(a) # TypeError: 'int' object is not callable
> 
> ```
> 
> 
> 
> I am using `collections.Iterator` as the means to check if the object is an iterator, however I am not sure if that is _root_ problem here. My understanding of the iterator protocol is that is assumes the __iter__ and next *methods* are implemented. In the example, `A.next` is defined as a property, but is still identified as an iterator. To me, this is incorrect behavior since it's not conforming to the iterator protocol requirements (i.e. a `next` method, not a property).
> 
> 
> 
> Raymond stated: "The design of ABCs are to check for the existence to required named; none of them verify the signature." I think I understand _why_ this is the case.. but I downstream libraries use `collections.Iterator` to determine if an object _is one_: see https://github.com/django/django/blob/master/django/utils/itercompat.py#L22-L31
> 
> 
> 
> Who's job is it to check if `next` (and technically `__iter__`) are methods?

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

collections.Iterator __subclasshook__ does not check if next() is callable Byron Ruth <bjruth@gmail.com> - 2013-03-31 10:57 -0700
  Re: collections.Iterator __subclasshook__ does not check if next() is callable Byron Ruth <bjruth@gmail.com> - 2013-03-31 12:44 -0700
  Re: collections.Iterator __subclasshook__ does not check if next() is callable Terry Jan Reedy <tjreedy@udel.edu> - 2013-03-31 15:47 -0400
    Re: collections.Iterator __subclasshook__ does not check if next() is callable Byron Ruth <bjruth@gmail.com> - 2013-03-31 12:58 -0700
    Re: collections.Iterator __subclasshook__ does not check if next() is callable Byron Ruth <bjruth@gmail.com> - 2013-03-31 12:58 -0700
  Re: collections.Iterator __subclasshook__ does not check if next() is callable Ethan Furman <ethan@stoneleaf.us> - 2013-03-31 17:02 -0700

csiph-web