A Suggestion (please read and respond)

D-Man dsh8290 at rit.edu
Mon Oct 30 10:08:42 EST 2000


Hi all!

Python is a great language and has many great features.  I especially like the dynamic typing and the flexibility it allows, in addition to its aid in distributed objects (CORBA).  

However, I think the total lack of declaration of variables reduces readability and can be an issue for new programmers.  For example:

>>> MyLongVariableName = 1
>>>#somewhere farther down in the code
>>>MyLongVAriableName = MyLongVariableName + 1

This is a problem!  (ok, so this example can be "fixed" with the new '+=' operator but other examples could be formed)

I think it can also be helpful for new programmers if variables must be declared first.

My proposal is to make a new keyword "var" (or some similar, but short and sweet word).  Variable names would have to be declared before they are used, but can be declared anywhere in the code.  Since the variable name is declared, it would then exist;  a value of "None" seems appropriate here.  I would like to see an exception to the declaration, however, in for loops.

example:

>>> var MyLongVariableName
>>> var AnotherVariable = 1
>>> for x in range( 10 ) :
..	pass
>>> AVar = 1  # error, AVar doesn't exist


Ok, so this would break existing code.  To solve this, some sort of compatibility switch could be added to the interpreter (for either way, to require declaration or to set compatibility).



What do people think of this proposal?   While thinking of it, there were several criteria I had in mind:

* keep it simple
* don't do anything radical (ex: the for loops are readable enough and should stay that way)
* allow the interpreter to pick up on mis-typings of variable names (ex in assignment)
* allow existing code to still work

Thanks for all the repsonses I'm sure I'll get.

-D





More information about the Python-list mailing list