variable declaration

DogWalker forestiero at qwest.net
Mon Jan 31 12:19:19 EST 2005


"EP" <EP at zomething.com> said:

>> ------------Original Message------------
>> From: Alexander_Zatvornitskiy at p131.f3.n5025.z2.fidonet.org (Alexander Zatvornitskiy)
>
>>
>> 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!
>
>
>Hmmm.  I am surely an expert in writing buggy code, but I can not say I make this error in Python.  Why is that?
>
>I'm not sure, but a couple things that may help me miss making this mistake in practice may be (somewhat informal in my case) unit testing - I test for correct results for at least a few cases.
>
>It may also help that with Python I can code at a somewhat higher conceptual level, or maybe it is just the syntax that helps avoid these problems:
>
>>>>  for epsilon in range (0,10):
>         S=S+epsilon
>      
>>>> for epsilon in range (0,10):
>	S=S+epselon
>	
>Traceback (most recent call last):
>  File "<pyshell#6>", line 2, in ?
>    S=S+epselon
>NameError: name 'epselon' is not defined
>
>
>It may seem like jumping off a cliff, but the improvement in readability (the variable declarations being visual clutter) makes it much easier for me to see my code, and any typos in it.
>
>It seems it would be simple enough to have one's code, or another script, automatically print out a sorted list of the variables - which would make the error you note obvious.  But I haven't needed this, yet at least.
>
>You might like Python and find the lack of variable declaration checking not a problem.  It's worth a shot.
>
>
class MyVars(object):
    __slots__ = ['epsilon', 'thud', 'foo']

mv = MyVars()

mv.epselon = 42
Traceback (most recent call last)

/home/dw/KirbyBase-1.7/<console>
      
AttributeError: 'MyVars' object has no attribute 'epselon'
      
mv.epsilon = 42


More information about the Python-list mailing list