3 ms·
Oh, and in true Python fashion, the reason it works is not because the REPL is special-casing "quit". It's because when you evaluate an expression, the REPL pri
by int_19h 8y ago
Oh, and in true Python fashion, the reason it works is not because the REPL is special-casing "quit". It's because when you evaluate an expression, the REPL prints out its repr. And `quit` is not a function, but rather a callable object with a custom repr:
>>> type(quit)
<class '_sitebuiltins.Quitter'>
>>> repr(quit)
'Use quit() or Ctrl-Z plus Return to exit'
and it's implemented like so:
class Quitter(object):
def __init__(self, name, eof):
self.name = name
self.eof = eof
def __repr__(self):
return 'Use %s() or %s to exit' % (self.name, self.eof)
def __call__(self, code=None):
# Shells like IDLE catch the SystemExit, but listen when their
# stdin wrapper is closed.
try:
sys.stdin.close()
except:
pass
raise SystemExit(code)
The same is done for "help", "copyright", "license" and "credits". REPL only does what its name says it does: read, evaluate, print, and loop.
So there's no magic. There's only Python.