Gripe: Use Ctrl-D (i.e. EOF) to exit

Dan Bishop danb_83 at yahoo.com
Thu Feb 26 02:48:08 CET 2004


jepler80 at unpythonic.net wrote in message news:<mailman.3.1077569287.8594.python-list at python.org>...
> Here's another workaround (also in ~/.pythonrc):
> 	class Exiter:
> 	    def __repr__(self): raise SystemExit
> 	quit = exit = Exiter()
> 
> $ python
...
> >>> exit
> $ 
> 
> Of course, *anything* that would print the repr of exit exits:
> >>> globals()
> $
> 
> This cure is worse than the disease, I think....

Here's an alternative that doesn't have that problem:

Python 2.3.3 (#2, Jan  4 2004, 12:24:16)
[GCC 3.3.3 20031229 (prerelease) (Debian)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> class Exiter:
...    def __repr__(self):
...       return 'Exiter()'
...
>>> def _displayhook(obj):
...    if isinstance(obj, Exiter):
...       raise SystemExit
...    elif obj is not None:
...       print repr(obj)
...       __builtins__._ = obj
...
>>> sys.displayhook = _displayhook
>>> quit = exit = Exiter()
>>> vars()
{'quit': Exiter(), '__builtins__': <module '__builtin__' (built-in)>,
'Exiter': <class __main__.Exiter at 0x40218a4c>, 'sys': <module 'sys'
(built-in)>, 'exit': Exiter(), '__name__': '__main__', '_displayhook':
<function _displayhook at 0x40217ed4>, '__doc__': None}
>>> quit
$



More information about the Python-list mailing list