Python Forum
Stupid Python Tricks: Q is for Quit
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Stupid Python Tricks: Q is for Quit
#1
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$ 
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#2
Overriding reprint for nefarious purposes.  That's fun.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  ? Headers: Keep-It=Simple; And=Stupid; With=Auto-Completion. Support HTTP, IMAP. Ousret 1 2,298 Apr-11-2020, 07:53 PM
Last Post: Ousret

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020