declaration of variables?
Paul Rubin
http
Sun Feb 16 18:26:31 EST 2003
Laura Creighton <lac at strakt.com> writes:
> Am I understanding you correctly? You don't want to bind a variable
> to a specific type, and you want to be able to rebind a variable at
> will to any other type, but you want a language change so that the
> first time you bind a variable, you have to type something extra
> indicating 'yes I know this is the first time I have used this
> variable?' If so, what does this buy you aside from catching typos?
For one thing, it lets you tell reliably whether a variable is global
or local. Consider this code, which sets a global and then uses its
value in the middle of a complicated function (example due to Andrew Koenig):
x = 3 # global variable
def f():
[some code]
if frob():
y = x # sets y to 3
[more code]
[still more code]
Now you edit the function for some reason:
def f():
[some code]
if frob():
y = x # oops!!!
[more code]
x = 5
[still more code]
Adding the "x = 5", possibly hundreds of lines into the function, has
changed x from a local into a global, so the "y = x" now does
something completely different from previously.
Catching typos is also perfectly worth doing. See for example the
"use strict" feature in perl.
More information about the Python-list
mailing list