is this a valid import sequence ?

Alex Martelli aleax at mac.com
Sat Jun 23 20:51:17 EDT 2007


Steven D'Aprano <steve at REMOVE.THIS.cybersource.com.au> wrote:

> On Sat, 23 Jun 2007 11:03:03 -0700, Scott David Daniels wrote:
> 
> > The global statement in Write_LCD_Data is completely unnecessary.  The
> > only time you need "global" is if you want to reassociate the global
> > name to another object (such as LCD = LCD + 1 or whatever).
> 
> That's technically true, but declaring it with global makes the code
> self-documenting and therefore easier to read.
> 
> It's never _wrong_ to use the global statement, even if it is strictly
> unnecessary for the Python compiler.

So, repeat that global statement ninetyseven times -- that's not
"wrong", either, in exactly the same sense in which it's not "wrong" to
have it once -- the Python compiler will not complain.  And by repeating
it over and over you make it less likely that a reader could miss it, so
it's even more "self-documenting" and "easier to read", right?

"Perfection is reached, not when there is no longer anything to
add, but when there is no longer anything to take away", as Antoine de
Saint-Exupery wrote.  Since that global statement is utterly useless
(it's impossible to read and understand any substantial amount of Python
code without realizing that accessing a variable not locally assigned
means you're accessing a global, so the "self-documenting" character
claimed for that redundancy is quite fallacious), it IS perfectly
suitable to take away, and so it's at least a serious imperfection.  It
violates Occam's Razor, by multiplying entities (specifically
statements) without necessity.  It's just about as bad as sticking a
semicolon at the end of every statement (to make it "self-documenting"
that the statement ends there), parentheses around the conditions in if
and while statements and the argument in return statements (to make it
"self-documenting" where those expressions start and end), and a few
other less common ways to waste pixels, screen space, readers' attention
spans, and everybody's time.  In other words, it's almost as bad as it
can get in Python without outright breakage of syntax or semantics
("almost" only because long comments that lie outright are worse:-).


Alex



More information about the Python-list mailing list