variable declaration

Carl Banks invalidemail at
Mon Jan 31 16:03:30 EST 2005

Thomas Bartkus wrote:
> Python *does* require that your variables be declared and initialized
> you use them. You did that with epsilon=0 and S=0 at the top.  It is
> unfortunate, however, that the statement epselon=epsilon+1 also
declares a
> new variable in the wrong place at the wrong time. Such mispellings
are a
> *common* error caught instantly in languages that require a more
> declaration procedure.

I have no interest in arguing this right now, but it does raise a
question for me:  How common is it for a local variable to be bound in
more than one place within a function?  It seems that it isn't (or
shouldn't be) too common.

Certainly the most common case where this occurs is for temporary
variables and counters and stuff.  These typically have short names and
thus are not as likely to be misspelled.

Another common place is for variables that get bound before and inside
a loop.  I would guess that's not as common in Python as it is in other
languages, seeing that Python has features like iterators that obviate
the need to do this.  (The OP's original example should have been "for
epsilon in range(10)"; epsilon only needed bound in one place.)

I guess this might be why, in practice, I don't seem to encounter the
misspelling-a-rebinding error too often, even though I'm prone to
spelling errors.  Perhaps, if someone runs into this error a lot, the
problem is not with Python, but with their tendency to rebind variables
too much?  Just a thought.


More information about the Python-list mailing list