On Sat, May 9, 2020 at 7:02 PM Ram Rachum <r...@rachum.com> wrote:

> Hey,
>
> I just found a good example for the unpacking errors I suggested:
>
>
>     def topological_sort(graph: networkx.DiGraph) -> tuple:
>         try:
>             (zero_node,) = (node for node in graph if not
> graph.neighbors(node))
>         except TooFewToUnpackError:
>             raise OverdefinedOrdering
>         except TooManyToUnpackError:
>             raise UnderdefinedOrdering
>
>         ...
>
> This is instead of the following code, which I find less elegant:
>
>     def topological_sort(graph: networkx.DiGraph) -> tuple:
>         zero_nodes = tuple(node for node in graph if not
> graph.neighbors(node))
>         if not zero_nodes:
>             raise OverdefinedOrdering
>         elif len(zero_nodes) >= 2:
>             raise UnderdefinedOrdering
>         else:
>             (zero_node,) = zero_nodes
>
>
> Besides elegance, the above code can be optimized with short-circuit logic
> for the TooManyToUnpackError, assuming that doesn't break backward
> compatibility.
>

The elegance argument doesn't convince me, you'll need to explain what you
mean by the last bit about short-circuit logic.
_______________________________________________
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/L7MPBSQLH63DHQLC7AOHG2LURXM4QRAK/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to