[Python-ideas] Integrate some itertools into the Python syntax
Michel Desmoulin
desmoulinmichel at gmail.com
Mon Mar 28 12:23:28 EDT 2016
Le 28/03/2016 18:13, Chris Barker a écrit :
> On Mon, Mar 28, 2016 at 7:45 AM, Franklin? Lee
> <leewangzhong+python at gmail.com <mailto:leewangzhong+python at gmail.com>>
> wrote:
>
> I'm still very uneasy about how slicing is usually random access,
> and doesn't change how it indexes its elements from repeated use. It
> means that you have something very different for the same syntax.
>
> I think only slightly different :-)
>
> On Mon, Mar 28, 2016 at 8:04 AM, Chris Angelico <rosuav at gmail.com
> <mailto:rosuav at gmail.com>> wrote:
>
>
> >>> range(10, 100)[25:35]
> range(35, 45)
>
> It's a slice. Whether it's random access or not is pretty much
> unrelated to slicing; you get a slice of the underlying object,
> whatever that is. A slice of a sliceable iterator should be a sliced
> iterator.
>
>
> sure -- but that works great for range, because is is an lazy evaluated
> sequence, which of course makes it iterable, but I think the trick is:
>
>
> arbitrary_iterable[10:20]
>
> should return an iterable that will return the 10th to the 20th item
> when iterated -- doable, but:
>
> for i in arbitrary_iterable[10:20]:
> pass
>
> will then have to call the underlying iterable 20 times -- if it's an
> iterator that isn't a sequence, its state will have been altered.
>
> so I'm not sure how to go about this -- but it would be nice.
>
> Also, arbitrary_iterable[-10:]
>
> would be essentially impossible.
It would be possible but would basically be equivalent to
list(arbitrary_iterable)[-10:] and would load everything in memory, at
best rendering generators useless, at worst exploding on a infinite data
stream.
But I think it would be ok to raise ValueError any negative indices by
default, and then let containers such as list/tuple define custom
behavior for them.
Note that arbitrary_iterable[:-10] or arbitrary_iterable[-10] would be
possibl, with collections.deque you buffer at most 10 elements in
memory, but I'm not sure if it would be preferable to fordid all
negative values, or have an special case. It's handy but confusing.
Also, what do you think about the callable passed as an index from the
first examples ?
>
> -CHB
>
>
> --
>
> Christopher Barker, Ph.D.
> Oceanographer
>
> Emergency Response Division
> NOAA/NOS/OR&R (206) 526-6959 voice
> 7600 Sand Point Way NE (206) 526-6329 fax
> Seattle, WA 98115 (206) 526-6317 main reception
>
> Chris.Barker at noaa.gov <mailto:Chris.Barker at noaa.gov>
>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
More information about the Python-ideas
mailing list