Python's BNF

MartinRinehart at MartinRinehart at
Fri Feb 29 14:21:48 CET 2008

Gabriel and Steve,

Poor globals! They take such a beating and they really don't deserve

The use of globals was deprecated, if memory serves, during the
structured design craze. Using globals is now considered bad practice,
but it's considered bad practice for reasons that don't stand close
scrutiny, this being a perfect example.

    ofile = ...

# global
def writeHTML():
    ofile.write( .. )
def writeBody():
    ofile.write( ... )
def writeEntries()
    ofile.write( ... )
def writeEntry():
    ofile.write( ... )

# "fixed" to eliminate the evil global
def writeHTML(ofile):
    ofile.write( .. )
def writeBody(ofile):
    ofile.write( ... )
def writeEntries(ofile)
    ofile.write( ... )
def writeEntry(ofile):
    ofile.write( ... )
# repeat above for another half dozen subs that also use ofile

The code's simpler before the fix.

So, as a nod to the anti-global school of thought, I changed 'ofile'
to 'OFILE' so that it would at least look like a global constant. Then
I changed to '_OFILE' as a reminder that this is a modular, not
global, constant. Ditto for '_PRODUCTIONS'. Modular constants share
exactly none of the coupling problems that globals can have. You'll
let me use modular constants, right?

More information about the Python-list mailing list