How much is set in stone?
Jive Dadson
jdadson at ix.netcom.com
Sun Nov 4 21:02:20 EST 2001
Thanks. Before I submit a formal proposal, let me run it by you guys.
I've looked through all the PEPS and I was surprised that my suggestion
is not already there. So, here it is. Please comment.
Other languages have started life with the feature that the first
occurrence of an identifier defines it. The first time a programmer
spends an afternoon chasing a bug introduced by misspelling an
identifier, he begins to doubt the wisdom of that feature. Suppose you
are called on to modify a long and complex module written by someone
else. At some point you forget that the original programmer did not
comply with your company's variable naming conventions. He named a
variable "keepRecord" when it should have been "keep_record". After
you've made your changes, the code usually works fine, but occasionally
it deletes a record it is supposed to keep. You stare at the code for
hours and you can see absolutely nothing wrong with it.
Here's another one. A programmer who is new to Python code might write
the following:
initialized = 0
def init_everything():
blah()
blah()
blah()
initialized = 1
# Oh noooooo!
I would have complete sympathy for this hypothetical unfortunate fellow.
Visual Basic (and perhaps other Basics, so far as I know) implement a
way to have your cake and yet not spoil the old cake. It will
correctly process code that is written to the old standard, but it
allows you to require a defining occurrence of an identifier. If you
want it the new way, you put a line at the top of your module that says,
option explicit
Of course you must know to put that in your source code, but most books
on Visual Basic tell you up front always to do it. It gets to be a
habit.
Here's how it might work in Python. Again the module would have
something at the top, like "option explicit" ("import explicit"? or
what?) telling the interpreter that within that module, variables must
be declared before use. Then the defining occurrence of a variable
could look similar to the declaration of a function:
option explicit
def initialized = 0
def init_everything():
blah()
blah()
blah()
initialized = 1
The above code would work as anticipated. No pesky "global" qualifier.
The following would generate an error message, just like trying to call
an undefined function does:
>>>option explicit
>>>initialized = 0
Traceback (most recent call last):
File "<stdin>", line 1, in ?
NameError: initialized is not defined.
After due warning (a year or two), "option explicit" could become the
default. Old code might need "option implicit" or something added at
the top to keep it working.
More information about the Python-list
mailing list