Larry Hastings wrote:
> On 11/23/20 8:15 AM, Brian Coleman wrote:
> > def process(root_node: Node):
> > def process_node(node: Node):
> > if isinstance(node, StringNode):
> > return node.value
> > elif isinstance(node, NumberNode):
> > return float(node.value)
> > elif isinstance(node, ListNode):
> > return [process_node(child_node) for child_node in
> > node.children]
> > elif isinstance(node, DictNode):
> > return {key: process_node(child_node) for key, child_node in
> > node.children}
> > else:
> > raise Exception('Unexpected node')
> > You don't need the "else". And you can change all your "elif"s into
> "if"s too. Now your "isinstance" version is 35 lines. Shorter than the
> pattern matching version, roughly the same speed, works in current
> Python, eminently readable to anybody conversant in current Python. A
> very reasonable solution to the problem.
> There should be one--and preferably only one--obvious way to do it,
> //arry/
It was not my intention to suggest that the solution implemented in the
shortest number of lines was superior.
I think that one of the advantages of the pattern matching implementation over
the sequence of `isinstance` checks is that there is no need to worry about
whether using `if`, `elif`or `else` is the best approach. There is a single
elegant and natural way to express the solution with pattern matching.
_______________________________________________
Python-Dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/7FCXBKZQ6PU7VAOB76GGFJJXFWEPXL7L/
Code of Conduct: http://python.org/psf/codeofconduct/