Python's BNF

MartinRinehart at gmail.com MartinRinehart at gmail.com
Fri Feb 29 08:21:48 EST 2008


Gabriel and Steve,

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

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
writeHTML()
def writeHTML():
    ofile.write( .. )
    writeBody()
def writeBody():
    ofile.write( ... )
    writeEntries()
def writeEntries()
    ofile.write( ... )
    writeEntry()
def writeEntry():
    ofile.write( ... )
...

# "fixed" to eliminate the evil global
writeHTML(ofile)
def writeHTML(ofile):
    ofile.write( .. )
    writeBody(ofile)
def writeBody(ofile):
    ofile.write( ... )
    writeEntries(ofile)
def writeEntries(ofile)
    ofile.write( ... )
    writeEntry(ofile)
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