Re:Lisp.py
by peatfreak on Wednesday July 20, 2022 @11:48AM
(#32167460)
I'm wondering about whether using exceptions in the utility functions is misusing Python's exception handling system... Look at is_integer() for example. Is catching ValueError -- if the token cannot be converted -- a Pythonic thing? Or is it abusing exceptions for ordinary (non-exceptional) tokenizing?
[ Parent
| on HN ]
Looking at the usage of is_integer, personally I would write something like parse_integer(x: str) -> Optional[int], where the None variant represents a caught ValueError; this avoids parsing the integer twice but achieves basically the same effect (in a very similar style).
What would you propose as "more pythonic" here? Writing your own integer parsing code, by looking at the ASCII values of each character in the string?
Re:Lisp.py
by dragonwriter on Wednesday July 20, 2022 @01:00PM
(#32168697)
It's suboptimal and you wouldn't want it for a production interpreter, but it's the lowest friction for leveraging what you get for free with Python to get something working correctly.