variable declaration

Thomas Bartkus tom at dtsam.com
Mon Jan 31 14:34:11 EST 2005


"Alexander Zatvornitskiy"
<Alexander_Zatvornitskiy at p131.f3.n5025.z2.fidonet.org> wrote in message
news:MSGID_2=3A5025=2F3.131_41fde342 at fidonet.org...
> Hello All!
>
> I'am novice in python, and I find one very bad thing (from my point of
view) in
> language. There is no keyword or syntax to declare variable, like 'var' in
> Pascal, or special syntax in C. It can cause very ugly errors,like this:
>
> epsilon=0
> S=0
> while epsilon<10:
>   S=S+epsilon
>   epselon=epsilon+1
> print S
>
> It will print zero, and it is not easy to find such a bug!
>
> Even Visual Basic have 'Option Explicit' keyword! May be, python also have
such
> a feature, I just don't know about it?

Exactly so!
Python *does* require that your variables be declared and initialized before
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 formal
declaration procedure.

Another irksome sitiuation is that while Python does enforce strict type
checking, you can re-declare variables and morph them in the middle of
nowhere.
     S = 0                       # It's an Integer!
     S = S + 'Hello'         # No can do! Strong type checking forbids this.
     S = 'GoodBye'         # Whoops - Now it's a string! Unfortunately
legal!
This seemingly demolishes all the good reasons one has for wanting strict
type checking.

That second example is just bad programming practice and easy to avoid.  The
problem you point out in your code, however, hurts! Was that an I, an l or a
1 in the variable name?

Hey! - I like Python a lot.
But nothings perfect
Thomas Bartkus





More information about the Python-list mailing list