feature suggestion

John Machin sjmachin at lexicon.net
Sat Dec 25 00:36:36 CET 2004

flexibal wrote:
> hi there.
> i didnt know if i should post it at python-dev or here, so i'll start
> here. i'd like to suggest a new language feature for python that
> you to explicitly declare a variable.
> as we all know, just doing
> v = 5
> declares a new variable named 'v'...

Does it? Who other than yourself comprise the "we" to whom you refer?

> but we are people, and we do make
> typos... and it's very disturbing to find your program crashing after
> two weeks of operation, over a NameError... because a certain branch
> your code, that was previously never taken, had finally been taken.

Google for "code coverage".

> i've written several large-scale projects in python by now... i find
> excellent for automated testing and other infrastructural needs

You need to test that your testing equipment works.

> ... but
> mistyped variable-, function-, class-, and module- names been a PITA
> for me ever since i've started to use python.
> python is a scripting language after all, and should preserve its
> scripting nature. but adding features that help you verify your
> code should also be included.
> BASIC has the "OPTION EXPLICIT" feature that forces you to explicitly
> declare all variables, and i see no reason why python should have a
> similar mechanism.

Most Python developers would agree with what you wrote, but not with
what the context shows you meant. Have you ever considered reading what
you have written before submitting it for execution? Google for "desk

> either you start python with some commandline switch, or with a
> language construct, you should be able to turn on explicit variable
> declaration.
> first proposal:
> second proposal:
> ================
> syntactic verification, at parsing time. for example:
> my-prog.py:
> -------------
> ##decl x
> ##decl y, z
> x = 5
> y = 6
> z = 8
> a = 6
> when this program will be ran with the -strict option, like 6th line
> will generate a SyntaxError: name 'a' not explicitly declared.

and also SyntaxError: name 's' declared but never used

Your proposal needs some augmentation:

(1) Have an environment variable DWIM_LEVEL -- if the edit distance
(with variable substitution penalties depending on keyboard proximity)
between a "not declared" name and a "declared but not used" name is
within the requested tolerance, then Python should assume equivalence
and continue silently.

(2) Recursively, this should be applied to the environment variable
itself, lest one type (say) SWIM_LEVEL or DIM_LEVEL.

(3) Have an ALTERNATE_METHOD_NAME option to cater for known problems
like "center" vs "centre", "-ise" vs "-ize", etc
I look forward to the discussion of your PEP.

More information about the Python-list mailing list