Re: variable declaration

From: EP (EP_at_zomething.com)
Date: 01/31/05


To: python-list@python.org
Date: Sun, 30 Jan 2005 23:19:13 -0800


> ------------Original Message------------
> From: Alexander_Zatvornitskiy@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.



Relevant Pages

  • Re: Why dont people like lisp?
    ... > Everybody CAN contribute to Python! ... If it were only about making small alterations to the syntax, ... full-fledged language constructs. ... Lisp-style Macros make these things a breeze. ...
    (comp.lang.python)
  • Re: Why dont people like lisp?
    ... > Everybody CAN contribute to Python! ... If it were only about making small alterations to the syntax, ... full-fledged language constructs. ... Lisp-style Macros make these things a breeze. ...
    (comp.lang.lisp)
  • Re: Why not adopt "Python Style" indentation for Ruby?
    ... *language* features where Python leads Ruby.) ... When you come right down to it, there are only a few basic syntax styles that have survived the test of time. ... I suppose assembly language syntax has also survived, and FORTRAN and COBOL, but I don't think anyone would design a new language with those. ...
    (comp.lang.ruby)
  • Re: Forth and Unix -- history
    ... can extend the compiler and the syntax if you want). ... I wouldn't call lack of built-in assembler a weakness, when there is good FFI, ... When you redefine syntax, you develop another language, and there're good ... in Python you can manipulate AST. ...
    (comp.lang.forth)
  • Re: Why dont people like lisp?
    ... To me it means the syntax is also domain specific. ... > Python is a domain specific language where the domain is ... A symbolic expression is a printed form which codes for a data ... > it isn't done because Lispers would just write the Python out ...
    (comp.lang.python)