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


Groups > comp.lang.python > #106253

Re: Strange range

From Rob Gaddi <rgaddi@highlandtechnology.invalid>
Newsgroups comp.lang.python
Subject Re: Strange range
Date 2016-04-01 20:21 +0000
Organization A noiseless patient Spider
Message-ID <ndml7n$494$1@dont-email.me> (permalink)
References (2 earlier) <ndlvrt$1hlp$1@gioia.aioe.org> <mailman.311.1459521083.28225.python-list@python.org> <87lh4xjt37.fsf@elektro.pacujo.net> <mailman.327.1459540903.28225.python-list@python.org> <874mblrsr1.fsf@elektro.pacujo.net>

Show all headers | View raw


Marko Rauhamaa wrote:

> Erik <python@lucidity.plus.com>:
>
>> On 01/04/16 15:34, Marko Rauhamaa wrote:
>>> Chris Angelico <rosuav@gmail.com>:
>>>
>>>> *A range object is not an iterator.*
>>>
>>> We now have learned as much.
>>>
>>> However, doesn't that extra level of indirection seem like an odd
>>> choice?
>>
>> [...]
>>
>> If you write your own class which has an __iter__ method, would you
>> [...] expect both iterators to be independent and both return the
>> 'foo', 'bar', 'baz' sequence (where that is the hypothetical result of
>> iterating over your object)?
>>
>> If you now replace MyClass() with range(10), why would you expect the
>> two iterators to be related?
>
> I simply had thought of range() returning an iterator. I would expect an
> iterator to behave like one.
>
> There's a bit of a cognitive dissonance between iterables and iterators.
> On the one hand, they behave identically in many contexts. On the other
> hand, the distinction is crucial in some special cases.

You're missing a key point.  All (well-behaved) iterators are iterables,
with their __iter__ method returning themselves.

for x in y:
  ...

implies:

try:
  _it = iter(y)
  while True:
    x = next(_it)
    ...
except StopIteration:
  pass

That's true for any iterable y, including a y which is itself an
iterator.  You still call iter() on it, and get _it, the iterator over
y, which if y is an iterator is the same thing as y.

-- 
Rob Gaddi, Highland Technology -- www.highlandtechnology.com

Email address domain is currently out of order.  See above to fix.

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


Thread

Strange range Marko Rauhamaa <marko@pacujo.net> - 2016-04-01 16:15 +0300
  Re: Strange range Chris Angelico <rosuav@gmail.com> - 2016-04-02 00:24 +1100
  Re: Strange range Steven D'Aprano <steve@pearwood.info> - 2016-04-02 00:26 +1100
    Re: Strange range Marko Rauhamaa <marko@pacujo.net> - 2016-04-01 17:12 +0300
      Re: Strange range Random832 <random832@fastmail.com> - 2016-04-01 10:39 -0400
    Re: Strange range Fabien <fabien.maussion@gmail.com> - 2016-04-01 16:16 +0200
      Re: Strange range Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-04-01 17:28 +0300
      Re: Strange range Chris Angelico <rosuav@gmail.com> - 2016-04-02 01:31 +1100
        Re: Strange range Marko Rauhamaa <marko@pacujo.net> - 2016-04-01 17:34 +0300
          Re: Strange range Jussi Piitulainen <jussi.piitulainen@helsinki.fi> - 2016-04-01 17:44 +0300
          Re: Strange range Chris Angelico <rosuav@gmail.com> - 2016-04-02 01:45 +1100
            Re: Strange range Marko Rauhamaa <marko@pacujo.net> - 2016-04-01 18:43 +0300
          Re: Strange range Erik <python@lucidity.plus.com> - 2016-04-01 20:58 +0100
            Re: Strange range Marko Rauhamaa <marko@pacujo.net> - 2016-04-01 23:14 +0300
              Re: Strange range Rob Gaddi <rgaddi@highlandtechnology.invalid> - 2016-04-01 20:21 +0000
                Re: Strange range Marko Rauhamaa <marko@pacujo.net> - 2016-04-01 23:44 +0300
              Re: Strange range Steven D'Aprano <steve@pearwood.info> - 2016-04-02 21:09 +1100
                Re: Strange range Marko Rauhamaa <marko@pacujo.net> - 2016-04-02 13:48 +0300
          Re: Strange range Ned Batchelder <ned@nedbatchelder.com> - 2016-04-02 12:47 -0700
            Re: Strange range Marko Rauhamaa <marko@pacujo.net> - 2016-04-02 23:44 +0300
              Re: Strange range Chris Angelico <rosuav@gmail.com> - 2016-04-03 07:05 +1000
                Re: Strange range Marko Rauhamaa <marko@pacujo.net> - 2016-04-03 00:40 +0300
                Re: Strange range Ned Batchelder <ned@nedbatchelder.com> - 2016-04-02 14:50 -0700
                Re: Strange range Stephen Hansen <me+python@ixokai.io> - 2016-04-02 23:43 -0700
                Re: Strange range Marko Rauhamaa <marko@pacujo.net> - 2016-04-03 09:58 +0300
                Re: Strange range Chris Angelico <rosuav@gmail.com> - 2016-04-03 17:10 +1000
                Re: Strange range Ethan Furman <ethan@stoneleaf.us> - 2016-04-03 09:28 -0700
                Re: Strange range Mark Lawrence <breamoreboy@yahoo.co.uk> - 2016-04-04 13:21 +0100
                Re: Strange range Chris Angelico <rosuav@gmail.com> - 2016-04-03 17:08 +1000
              Re: Strange range Steven D'Aprano <steve@pearwood.info> - 2016-04-03 14:43 +1000
                Re: Strange range Random832 <random832@fastmail.com> - 2016-04-03 01:20 -0400
                Re: Strange range Steven D'Aprano <steve@pearwood.info> - 2016-04-03 15:28 +1000
      Re: Strange range Marko Rauhamaa <marko@pacujo.net> - 2016-04-01 17:32 +0300
  Re: Strange range Random832 <random832@fastmail.com> - 2016-04-01 10:42 -0400
  Re: Strange range Chris Angelico <rosuav@gmail.com> - 2016-04-02 01:50 +1100
  Re: Strange range Ian Kelly <ian.g.kelly@gmail.com> - 2016-04-01 08:52 -0600

csiph-web