3 ms·
What does Python do these days? I seem to remember it would catch common cases, but rather than quit, scold you and tell you how you should be doing it the othe
by peteretep 8y ago
What does Python do these days? I seem to remember it would catch common cases, but rather than quit, scold you and tell you how you should be doing it the other way, which seemed like the most Western European approach imaginable.
- int_19h 8y agoPython 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.
- ahartmetz 8y agoPython actually taught me to use Ctrl-D everywhere. Even though I was initially annoyed, I now understand the wisdom behind it :P
- int_19h 8y agoExcept it's Ctrl+Z in Python for Windows, because that's the convention for EOF there.