On 11/16/2020 6:14 AM, Mark Shannon wrote:
2. Is the error in the ast matching example, an intentional
"simplification" or just an oversight?
The example:
```
def simplify(node):
match node:
case BinOp(Num(left), '+', Num(right)):
return Num(left + right)
case BinOp(left, '+' | '-', Num(0)):
return simplify(left)
case UnaryOp('-', UnaryOp('-', item)):
return simplify(item)
case _:
return node
```
is wrong.
This is not 'wrong' because the paper did not specify the signatures or
definition of BinOp and UnaryOp or a specific module/package of origin.
I took these as hypothetical classes in a hypothetical module that
happens to use what I presume are ast class names.
The correct version is
```
def simplify(node):
match node:
case BinOp(Num(left), Add(), Num(right)):
return Num(left + right)
case BinOp(left, Add() | Sub(), Num(0)):
return simplify(left)
case UnaryOp(USub(), UnaryOp(USub(), item)):
return simplify(item)
case _:
return node
```
Without knowing the signatures of the ast classes, having not worked
with them as you have, passing instances of an Add class instead of an
'add' function looks wrong. How is one Add() different from another?
For the didactic purpose of the example, this looks worse to me. For me,
it is harder to read.
--
Terry Jan Reedy
_______________________________________________
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/HH26LZOEN446UWKP3J3OFWZ7AYMV2F5A/
Code of Conduct: http://python.org/psf/codeofconduct/