Hi Caleb,

On Tue, Feb 23, 2021 at 11:05 PM Caleb Donovick <donov...@cs.stanford.edu>
wrote:

> What is the motivation for returning `None` on empty splits?  I feel like
> this creates an unnecessary asymmetry.  I don't personally have a use
> case for the this feature so I may be missing something but it seems like
> it would force an annoying pattern:
>

Split is used by the interpreter to implement except*. It needs to check
whether the first value of the split is empty or not (this determines
whether the except* clause executes).
This is more efficiently done with None. And then there is creating an
object that you don't need.

The intention is that you use except* rather than do this:


> ```
> try:
>     foo()
> except ExceptionGroup as eg:
>     g1, g2 = eg.split(predicate)
>     for e in g1:
>         handle_true(e)
>     for e in g2:
>         handle_false(e)
> ```
>

(as an aside - exception groups ended up not being iterable - that is one
of the rejected ideas, see explanation there).


> Also this creates an subtle difference with subgroup:
>

> ```
> g1, g2 = eg.split(predicate)
> h1, h2  = eg.subgroup(predicate), eg.subgroup(lambda e: not predicate(e))
> assert g1 == h1 and g2 == h2 # only true if `None not in {g1, g2}`
> ```
>

An empty subgroup should also return None. I see this is not mentioned in
the PEP, so I will add it. Thanks.

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

Reply via email to