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.
...
On Sun, May 3, 2020 at 1:39 AM Soni L.
On 2020-05-02 7:29 p.m., Steven D'Aprano wrote:
On Sat, May 02, 2020 at 03:39:50PM -0300, Soni L. wrote:
def foo(): yield from () raise ValueError
def foo(): yield from () raise ValueUnpackingError
Does that help?
the idea is that it'd become a RuntimeError when you unpack it. _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@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/GQVFED... Code of Conduct: http://python.org/psf/codeofconduct/