variable declaration

Alexander Alexander_Zatvornitskiy at p131.f3.n5025.z2.fidonet.org
Tue Feb 8 05:11:28 EST 2005


Hi, Peter!

05 feb 2005 at 15:28, Peter Otten wrote:

 >> variable names. You have to move the code into a function, though: $
 >> cat epsilon.py ...skipped... $ pychecker epsilon.py epsilon.py:6:
 >> Local variable (epselon) not used Well, I can change it a little to
 >> pass this check. Just add "print epselon" line.

 PO> You are now on a slippery slope. I'd rather think of ways to write my
 PO> code in a way for it to succeed or at least fail in an obvious way.
Ok, fixed.
 PO> I don't consider a scenario likely where you both misspell a name
 PO> nearly as often as you write it correctly, and do that in a situation
 PO> where the program enters an infinite loop instead of just complaining
 PO> with an exception, which is by far the most likely reaction to a
 PO> misspelt name.
Let say, you have variable or class field 'PowerOfGenerator', or 'lambda_k'.
You decide to rename it to 'ReactivePowerOfGenerator' or 'lambda_kx'. But, for
some reasons, you forget to do it everywhere, for example in function with
first lines like this:

  PowerOfGenerator=TakeFromSensor()
  if PowerOfGenerator>xxx:
    ....
  RecalcPower(PowerOfGenerator)
  PutToTheDatabase(PowerOfGenerator)
  ....
Here, python will not help you. The worst thing is that in such calculations
you often receive plausible results.

Another example. Let say you have variable PowerOfGenerator in your program.
But, it is only active power, so you have to (1)rename PowerOfGenerator to
ActivePowerOfGenerator, (2)calculate ReactivePowerOfGenerator, and (3)calculate
new PowerOfGenerator by formula
  PowerOfGenerator=sqrt(ReactivePowerOfGenerator**2+ActivePowerOfGenerator**2)
With var declarations, on step (1) you just rename PowerOfGenerator to
ActivePowerOfGenerator in the place of its declaration, and compile your
program. Compiler will show you all places where you have to rename variables.
After it, on step (3) you can safely and peacefully add new PowerOfGenerator
variable.

Of course, I am trying to use more short variable names, but its just an
example.

 PO> Code not written is always errorfree, and in that spirit I'd rather
 PO> strive to write the function more concisely as

 PO> def loop2():
 PO>     s = 0
 PO>     for delta in range(10):
 PO>         s += delta
 PO>         print s

 PO> This illustrates another problem with your approach: would you have
 PO> to declare globals/builtins like range(), too?
if I understand you right, it can be done this way (with '~' instead of 'var'):
  ~s=0
  for ~delta in range(10)
    s+=delta

[skip]

 PO> I suggested pychecker more as a psychological bridge while you gain
 PO> trust in the Python way of ensuring reliable programs, i. e. writing
 PO> small and readable functions/classes that do one thing well and can
 PO> easily be tested. Administrative overhead -- as well as excessive
 PO> comments -- only serve to bury what is actually going on.

Your explanation is most "non-religious" in comparison with all others I see in
this newsgroup. Thank you, now I see that lack of var declarations is not
stupid oversight, but it may have reasons.
>From the other side, I still think that optional var declaration is good idea,
for example in functions which are big and operate with tens of variables,
non-decomposable by their nature. Such functions occurs in some domains. And,
in situations like above.

 PO> I guess that means no, not a good idea.

 PO> On the other hand, taking all names used in a function and looking
 PO> for similar ones, e. g. by calculating the Levenshtein distance,
 PO> might be worthwhile...

Hmm, what is the Levenshtein distance between 'x1' and 'x2'? Or between kappa_i
and kappa_j? :)

Alexander, zatv at bk.ru



More information about the Python-list mailing list