I love Python. But I do have a few pet-peeves. This is my solution to one of them.

When you type exit into an interactive session Python responds like this:

Python 2.2.1 (#34, Apr  9 2002, 19:34:33) [MSC 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> exit
'Use Ctrl-Z plus Return to exit.'
>>>

It knows exactly what I'm trying to do, but instead of just doing it, I get a message telling me to do it my self. Add the code below to your pythonstartup file and exit will work as you'd expect!

code

   1 class exitstring:
   2         def __repr__(self):
   3                 raise SystemExit
   4 
   5 exit = exitstring()
   6 quit = exitstring()

comments on the code

First, I create a small dummy class which exits the interpreter when printed. Then I make both exit and quit instances of that class. Next time you type one of them, *poof*, you're back at your shell.


Actually, it doesn't know what you are trying to do, the variable exit has just been defined to contain the string telling you to use Ctrl-Z. This way, if somebody fires up the interpreter and does:

>>> exit = 'foo'
>>> exit

The right thing happens, (i.e. the value of exit is printed, rather than the interpreter exiting) -- WilhelmFitzpatrick

Even if the default were changed to cause exit to actually exit, the user could still replace exit with 'foo' and have the value 'foo' printed, correct? -- BrianDorsey

PythonRealExit (last edited 2008-03-04 08:33:13 by localhost)