Grieve (grieve) wrote,
Grieve
grieve

Python

I often use python for one-of scripts, and home projects. I often learn new things in it, since I have never formally studied it, but today I learned one which I thought was hilarious. If you invoke python from the command line you get something that looks like this:
Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

Notice that the command prompt is ">>>", well it turns out that the command prompt is a value in sys. So you can do the following:
>>> import sys
>>> sys.ps1 = "MyNewPrompt>>"
MyNewPrompt>>

But it gets better! You can create a python module (just a py file), and it can override the prompt as well. So it looks something like this:
Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import insult
What next, idiot?for x in range(9):
Now What?       print x
Now What?
0
1
2
3
4
5
6
7
8
What next, idiot?

Notice that the secondary prompt, normally "...", can also be overridden. It is sys.ps2, as you may have already guessed. But it gets better! It turns out that you don't have to just pass it a string. Anything that supports the __str__() method will work.

giving something like:
Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import insult
           hack>>>
       you suck>>>
    emacs blows>>>
    Stupid jerk>>>
    emacs blows>>>
  Script kiddie>>>

So you can add this to all of your python modules, and let the fun ensue. G. who showed me this trick, pointed out that you could use it to make the prompt display the current time, but that seemed totally useful, and therefore pointless to me.
Tags: programming
Subscribe
  • Post a new comment

    Error

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 0 comments