On Tue, 14 Sept 2021 at 19:58, Tim Peters <tim.pet...@gmail.com> wrote:

> Except it's not that simple:

Apologies, Tim, it took me a couple of reads to work out what you were
saying here. I hope you won't mind if I restate the point for the
benefit of anyone else who might have got confused it like I did...

>     def gen(hi):
>         i = 0
>         while i < hi:
>             yield i
>             i += 1
>
>     from itertools import compress
>     g = gen(12)
>     print(list(filter(None, g)))
>     g = gen(12)
>     print(list(compress(g, g)))
>
> Which displays:
>
>     [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
>     [0, 2, 4, 6, 8, 10]
>
> The first is obviously intended, but the latter is what you get merely
> by giving the same argument twice to `complress()`.

The key point here is that the proposal that compress(g) should mean
the same as compress(g, g) doesn't actually do what you were
suggesting it would (and what you want), if g is an iterator - and
after all, that's what itertools is supposed to be about, operations
on iterators in general.

To actually get the same behaviour as filter(None, g) in general needs
a much more complicated change than simply saying "the default is to
use the first argument twice".

> `compress()` can't materialize its argument(s) into a list (or tuple)
> first, because it's intended to work fine with infinite sequences. It
> could worm around that like so:under the covers:
>
>     from itertools import tee
>     g = gen(12)
>     print(list(compress(*tee(g))))
>
> but that's just bizarre ;-) And inefficient.
>
> Or perhaps the `compress()` implementation could grow internal
> conditionals to use a different algorithm if the second argument is
> omitted. But that would be a major change to support something that's
> already easily done in more than one more-than-less obvious way.

At this point, it's no longer a simple change adding a fairly obvious
default value for an argument that's currently mandatory, it's
actually an important and significant (but subtle) change in
behaviour.

Honestly, I think this pretty much kills the proposal. Thanks for
pointing this flaw out Tim, and sorry if I laboured the point you were
making :-)

Paul
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/4N2UXKQBLAFM3OWVBTQ572SJZOU5PM5G/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to