[Python-ideas] No implicit variable declarations
Markus Unterwaditzer
markus at unterwaditzer.net
Sat Apr 12 19:47:42 CEST 2014
Hello James,
I see how this would help with avoiding a whole class of errors, but then i
also think your two suggestions -- the var and implicit keywords -- would take
a great deal of flexibility and expressiveness from the language. Also, Python
was never meant to be statically typed, so suggesting static typing so many
years after its creation is IMO pointless, even though you're suggesting it to
be optional. OTOH I like your idea of suggesting variable names when NameError
is raised, but i don't know how easy this is to implement in CPython.
I also want to say that the kind of error you're describing never occured to me
in practice. The mistake of assigning to the wrong variable name (and not
noticing it immediately through an exception) seems to me like something that
only occurs when deliberately writing huge cludges of spaghetti-code, or when
you're simply not using enough namespaces.
-- Markus
On Sat, Apr 12, 2014 at 11:14:18AM -0600, James Nelson wrote:
> There are several problems with Python's implicit variable declaration.
> Just one tiny mistake, like assigning to SpamEggs instead of spamEggs will
> create a new variable SpamEggs, and spamEggs will hold a stale value.
>
> Two statements should be added to Python: (1) a statement that specifies
> that implicit declaration by assignment should be disabled, like Fortran's
> "implicit none", and (2) a variable declaration statement, a la "var
> spamEggs". Trying to assign to a non-existent variable would, of course,
> cause an error. If the non-existent variable's name is close enough to an
> existing variable's name, the interpreter might suggest the correct name:
> "Did you mean "spamEggs"?".
>
> This idea could even open Python to static typed variables, declared like
> "var spamEggs as int". This would prevent the stupid but very possible
> error of assigning "clientName" (a string) to "doorStatus" (a boolean).
>
> -rb
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
More information about the Python-ideas
mailing list