This idea is something I could have used many times. I agree with many
people here that the strict=True API is at least "unusual" in Python. I was
thinking of 2 different API approaches that could be used for this and I
think no one has mentioned:


   - we could add a callable filler_factory keyword argument to zip_longest.
   That would allow passing a function that raises an exception if I want
   "strict" behaviour, and also has some other uses (for example, if I want to
   use [] as a filler value, but not the *same* empty list for all fillers)
   - we could add methods to the zip() type that provide different
   behaviours. That way you could use zip(seq, seq2).shortest(), zip(seq1,
   seq2).equal(), zip(seq1, seq2).longer(filler="foo") ; zip(...).shortest()
   would be equivalent to zip(...).  Other names might work better with
   this API, I can think of zip(...).drop_tails(), zip(...).consume_all() and
   zip(...).fill(). This also allows adding other possible behaviours (I
   wouldn't say it's common, but at least once I've wanted to zip lists of
   different length, but get shorter tuples on the tails instead of fillers).




On Mon, 20 Apr 2020 at 18:44, Ram Rachum <r...@rachum.com> wrote:

> Here's something that would have saved me some debugging yesterday:
>
>     >>> zipped = zip(x, y, z, strict=True)
>
> I suggest that `strict=True` would ensure that all the iterables have been
> exhausted, raising an exception otherwise.
>
> This is useful in cases where you're assuming that the iterables all have
> the same lengths. When your assumption is wrong, you currently just get a
> shorter result, and it could take you a while to figure out why it's
> happening.
>
> What do you think?
> _______________________________________________
> 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/6GFUADSQ5JTF7W7OGWF7XF2NH2XUTUQM/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
_______________________________________________
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/G7KMUNO6QQBYFHDPII4TW3LMCLFGWZOY/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to