3 ms·
Except they didn't -- the result of a statement is turned into a string, and the string is printed. There are standard ways of turning objects into strings, an
by andrewaylett 3y ago
Except they didn't -- the result of a statement is turned into a string, and the string is printed. There are standard ways of turning objects into strings, and the `__repr__` function on the `exit` object returns that string. If you call that object then it raises an exception that triggers a REPL to cleanly quit.
The code is here: https://github.com/python/cpython/blob/3.12/Lib/_sitebuiltins.py#L18 https://github.com/python/cpython/blob/3.12/Lib/_sitebuiltin...
- saulpw 3y agoHow about making `exit.__repr__` raise a CleanExit exception that gets caught in the outer REPL and then exits?
- andrewaylett 3y agoI'm fairly sure that having any object's `__repr__` throw an exception and exit would lead to even more confusion. Especially if it exits cleanly. The object shouldn't be in scope anywhere other than the REPL, but that doesn't mean that something, somewhere, isn't stringifying everything because $REASONS and changing the behaviour won't cause an obscure "crash" somewhere unexpected and hard to debug.