3 ms·
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more inform
by int_19h 8y ago
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:59:51) [MSC v.1914 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> quit
Use quit() or Ctrl-Z plus Return to exit
>>> exit
Use exit() or Ctrl-Z plus Return to exit
- int_19h 8y agoOh, 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.