Hacker News new | ask | show | jobs
by andrewaylett 899 days 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/_sitebuiltin...

1 comments

How about making `exit.__repr__` raise a CleanExit exception that gets caught in the outer REPL and then exits?
I'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.