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


Groups > comp.lang.python > #42400

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

Path csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <bjruth@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'programmer': 0.03; 'subject:not': 0.03; 'responding': 0.07; 'statically': 0.07; 'assumed': 0.09; 'attributes': 0.09; 'implemented.': 0.09; 'method,': 0.09; 'methods,': 0.09; 'rejected': 0.09; 'to:addr:comp.lang.python': 0.09; 'url:github': 0.09; 'cc:addr :python-list': 0.11; 'python': 0.11; 'def': 0.12; 'bug': 0.12; 'jan': 0.12; '@property': 0.16; 'a()': 0.16; 'a(object):': 0.16; 'backwards': 0.16; 'byron': 0.16; 'callable': 0.16; 'collections': 0.16; 'competent': 0.16; 'conforming': 0.16; 'downstream': 0.16; 'incorrect': 0.16; 'iterator': 0.16; 'iterator,': 0.16; 'justified': 0.16; 'likely.': 0.16; 'programmer,': 0.16; 'reedy': 0.16; 'renamed': 0.16; 'ruth': 0.16; 'subject:skip:_ 10': 0.16; 'typeerror:': 0.16; 'wrote:': 0.18; 'properly': 0.19; '(the': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; 'header:User- Agent:1': 0.23; '31,': 0.24; 'errors.': 0.24; 'skip:` 20': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'least': 0.26; 'defined': 0.27; 'header:In-Reply-To:1': 0.27; 'url:bugs': 0.29; '(this': 0.29; 'raymond': 0.30; 'that.': 0.31; 'accidentally': 0.31; 'assumes': 0.31; 'behaving': 0.31; 'comparison': 0.31; 'concern': 0.31; 'existence': 0.31; 'libraries': 0.31; 'subject:next': 0.31; 'class': 0.32; 'checked': 0.32; 'url:python': 0.33; '(i.e.': 0.33; 'alone': 0.33; 'checking': 0.33; 'implemented': 0.33; 'not.': 0.33; '"the': 0.34; 'skip:_ 10': 0.34; 'problem': 0.35; 'possible.': 0.35; 'problem.': 0.35; 'definition': 0.35; "who's": 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'doing': 0.36; 'next': 0.36; 'method': 0.36; 'thanks': 0.36; 'possible': 0.36; 'url:org': 0.36; 'should': 0.36; 'example,': 0.37; 'requirements': 0.37; 'being': 0.38; 'feedback': 0.38; 'initially': 0.38; 'fact': 0.38; 'pm,': 0.38; 'does': 0.39; 'realize': 0.39; 'sure': 0.39; 'even': 0.60; 'skip:u 10': 0.60; 'march': 0.61; 'complete': 0.62; 'more': 0.64; 'determine': 0.67; 'realized': 0.68; 'saw': 0.77; 'behavior': 0.77; 'hardly': 0.84; 'investigated': 0.84; 'subject:check': 0.84; 'technically': 0.84; 'url:master': 0.84; 'worthwhile.': 0.84; 'time)': 0.91; '2013': 0.98
X-Received by 10.49.116.165 with SMTP id jx5mr638725qeb.22.1364759895579; Sun, 31 Mar 2013 12:58:15 -0700 (PDT)
Newsgroups comp.lang.python
Date Sun, 31 Mar 2013 12:58:15 -0700 (PDT)
In-Reply-To <mailman.4025.1364759240.2939.python-list@python.org>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=65.78.73.157; posting-account=nhAoTgoAAAAiTaZekt1AoJpN1Rr2agdx
References <a108caf6-538b-4ca0-978a-97bacc7ca7e8@googlegroups.com> <mailman.4025.1364759240.2939.python-list@python.org>
User-Agent G2/1.0
X-Google-Web-Client true
X-Google-IP 65.78.73.157
MIME-Version 1.0
Subject Re: collections.Iterator __subclasshook__ does not check if next() is callable
From Byron Ruth <bjruth@gmail.com>
To comp.lang.python@googlegroups.com
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Message-ID <mailman.4026.1364759904.2939.python-list@python.org> (permalink)
Lines 147
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1364759904 news.xs4all.nl 6841 [2001:888:2000:d::a6]:55621
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:42400

Show key headers only | View raw


Thanks for responding Terry.

I can assure you I did not initially realize both the `next` and the `__iter__` methods were implemented when I ran into my original problem. I saw a behavior and had to work backwards to realize why it was behaving the way it was (the comparison against Iterator). Once I realized this, the behavior made complete sense. It just dawned on me the fact that `next` was not being checked to be callable (I was surprised by this at the time) which is why I investigated the `Iterator.__subclasshook__` and assumed it was behaving incorrectly based on my assumptions.

On Sunday, March 31, 2013 3:47:07 PM UTC-4, Terry Jan Reedy wrote:
> On 3/31/2013 1:57 PM, 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:
> 
> 
> 
> A competent Python programmer should not do that. In Py3, the method is 
> 
> properly renamed '__next__', which should make doing that accidentally 
> 
> even less likely.
> 
> 
> 
> >
> 
> > ```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,
> 
> 
> 
> Being an Iterator only means that it *might* be 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).
> 
> 
> 
> There is more to any protocol than can be statically checked.
> 
> 
> 
> > Raymond stated: "The design of ABCs are to check for the existence to required named; none of them verify the signature."
> 
> 
> 
> Having the required attributes is currently the definition of being an 
> 
> instance of an ABC. Adding 'not a property' would be possible. but 
> 
> hardly worthwhile. Checking signatures would be worthwhile, but 
> 
> signatures are not yet available to Python for C-coded methods, let 
> 
> alone other implementations.
> 
> 
> 
>   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?
> 
> 
> 
> The programmer, and a user who does not trust the competence of the 
> 
> programmer. But this is the least of the possible errors.
> 
> 
> 
> --
> 
> Terry Jan Reedy

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