|
From: | Dmitry Gutov |
Subject: | Re: Python REPL using standard library functions |
Date: | Tue, 3 Nov 2020 00:46:12 +0200 |
User-agent: | Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 |
On 02.11.2020 13:40, Yuri Khan wrote:
On Mon, 2 Nov 2020 at 16:50, Dmitry Gutov <dgutov@yandex.ru> wrote:On 02.11.2020 08:14, Yuri Khan wrote:Python has ast.parse(), compile() and exec(), all three exposed in the standard library.I'd be willing to bet that any given Python REPL doesn't use them, though.One of the guiding principles of Python is “There must be one — and preferably only one — obvious way to do it”. Implementing a REPL in terms of ast.parse, compile() and exec is one obvious way to do it. There is a catch that compile() with an option argument can be used instead of ast.parse, so that’s another obvious way to do it. Both are fully exposed to the user though.
Is eval implemented in terms of either?
Indeed, my limited experimentation shows I can augment these built-in functions and they are immediately picked up by at least one REPL:
The basic Python REPL doesn't seem to use it (your experiment doesn't result in anything being printed). Perhaps ipython uses ast.parse for syntax highlighting?
Ruby's standard library also includes a parser. The 'eval' method doesn't seem to be implemented in terms of it. At least, not directly.
In any case, I disagree that whether something is a REPL or not, should be decided by its implementation details.
[Prev in Thread] | Current Thread | [Next in Thread] |