Python Poetry: the __main__ Stanza

By Xah Lee. Date:

here's a snippet of Python doc:

This module represents the (otherwise anonymous) scope in which the interpreter's main program executes — commands read either from standard input, from a script file, or from an interactive prompt. It is this environment in which the idiomatic “conditional script” stanza causes a script to run:

if __name__ == "__main__":

from 27.5. __main__ — Top-level script environment — Python v2.7.6 documentation #module-__main__

notice the word “stanza”? Here's a technical writing in a technical manual, and in comes a technical term of poetry. This must be a technical humor.

from the few writings of Guido i've seen, am gussing Guido wrote this piece. His writing's always a bit odd.

also, Python __main__ is a prototypical hack. It came into being because Python's trying to have a consistent namespace scheme: module has namespace, therefore global stuff must belong to some “module” too, thus this pseudo-module __main__ was born.

from which, we begot this common nonplus:

if __name__ == "__main__":
    print "I ran as script!"

If you have a question, put $5 at patreon and message me.