Python Forum

Full Version: Stupid Python Tricks: Q is for Quit
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I write a lot of text interfaces, probably because I'm too lazy to write a GUI unless I need one. So I am frequently programming 'q' to quit the program. And I am frequently using 'q' to quit a program. So I frequently use 'q' to quit Python.

Error:
craig@craig-Latitude-E5440:~/Documents/Python$ python3 Python 3.5.2 (default, Nov 17 2016, 17:05:23) [GCC 5.4.0 20160609] on linux Type "help", "copyright", "credits" or "license" for more information. >>> q Traceback (most recent call last):   File "<stdin>", line 1, in <module> NameError: name 'q' is not defined
I decided to fix this deficiency:

import sys

class Quitter(object):
    """
    Quit on q. (object)

    Overridden Methods:
    __repr__
    """

    def __repr__(self):
        """Quit on q. (None)"""
        sys.exit()
q = Quitter()
Usage:

craig@craig-Latitude-E5440:~/Documents/Python$ python3 
Python 3.5.2 (default, Nov 17 2016, 17:05:23) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> q
craig@craig-Latitude-E5440:~/Documents/Python$ 
Overriding reprint for nefarious purposes.  That's fun.