Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > variable declaration

Reply
Thread Tools

variable declaration

 
 
Alexander Zatvornitskiy
Guest
Posts: n/a
 
      01-31-2005
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?

Alexander,
 
Reply With Quote
 
 
 
 
EP
Guest
Posts: n/a
 
      01-31-2005
> ------------Original Message------------
> From: rg (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.



 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      01-31-2005
Alexander Zatvornitskiy wrote:

> 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!


pychecker may help you find misspelled variable names. You have to move the
code into a function, though:

$ cat epsilon.py
def loop():
epsilon=0
S=0
while epsilon<10:
S=S+epsilon
epselon=epsilon+1
print S

$ pychecker epsilon.py
Processing epsilon...

Warnings...

epsilon.py:6: Local variable (epselon) not used

Peter

 
Reply With Quote
 
Alex Martelli
Guest
Posts: n/a
 
      01-31-2005
Alexander Zatvornitskiy
<. org> wrote:

> 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


Since the lack of declarations is such a crucial design choice for
Python, then, given that you're convinced it's a very bad thing, I
suggest you give up Python in favor of other languages that give you
what you crave. The suggestions about using pychecker, IMHO, amount to
"band-aids" -- the right way to deal with minor annoying scratches, but
surely not with seriously bleeding wounds, and your language ("very bad
thing", "very ugly errors") indicates this is a wound-level issue for
you. Therefore, using Python, for you, would mean you'd be fighting the
language and detesting its most fundamental design choice: and why
should you do that? There are zillions of languages -- use another one.

> 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!


Actually, this while loop never terminates and never prints anything, so
that's gonna be pretty hard to ignore. But, assume the code is
slightly changed so that the loop does terminate. In that case...:

It's absolutely trivial to find this bug, if you write even the tiniest
and most trivial kinds of unit tests. If you don't even have enough
unit tests to make it trivial to find this bug, I shudder to think at
the quality of the programs you code. Even just focusing on typos,
think of how many other typos you could have, besides the misspelling of
'epsilon', that unit tests would catch trivially AND would be caught in
no other way whatsoever -- there might be a <= where you meant a <, a
1.0 where you meant 10, a - where you meant a +, etc, etc.

You can't live without unit tests. And once you have unit tests, the
added value of declarations is tiny, and their cost remains.

A classic reflection on the subject by Robert Martin, a guru of C++ and
other languages requiring declarations, is at:
<http://www.artima.com/weblogs/viewpost.jsp?thread=4639>.


> Even Visual Basic have 'Option Explicit' keyword! May be, python also have
> such a feature, I just don't know about it?


Python has no declarations whatsoever. If you prefer Visual Basic, I
strongly suggest you use Visual Basic, rather than pining for Visual
Basic features in Python. If and when your programming practices ever
grow to include extensive unit-testing and other aspects of agile
programing, THEN you will be best advised to have a second look at
Python, and in such a case you will probably find Python's strengths,
including the lack of declarations, quite compelling.

Some people claim a language should change the way you think -- a
frequent poster, excellent Python contributor, and friend, even has that
claim in his signature. That may be alright in the groves of academia.
If you program to solve problems, rather than for personal growth, on
the other hand, changing "the way you think" with each programming
language is a rather steep price to pay. As a pragmatist, I prefer a
motto that I've also seen about Python: "it fits your brain". I find
it's true: Python gets out of my way and let me solve problems much
faster, because it fits my brain, rather than changing the way I think.

If Python doesn't fit YOUR brain, for example because your brain is
ossified around a craving for the declaration of variables, then, unless
you're specifically studying a new language just for personal growth
purposes, I think you might well be better off with a language that
DOES, at least until and unless your brain changes by other means.


Alex
 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      01-31-2005
Alex Martelli wrote:

> Alexander Zatvornitskiy
> <. org> wrote:
>
>
>>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

>

[...]
There are zillions of languages -- use another one.
[...]
> If Python doesn't fit YOUR brain, for example because your brain is
> ossified around a craving for the declaration of variables, then, unless
> you're specifically studying a new language just for personal growth
> purposes, I think you might well be better off with a language that
> DOES, at least until and unless your brain changes by other means.
>
>
> Alex


I think we should all remember that Python isn't for everyone, and least
of all for those with little knowledge of Python and preconceptions
about what Python *should* be like.

regards
Steve
--
Steve Holden http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/
Holden Web LLC +1 703 861 4237 +1 800 494 3119
 
Reply With Quote
 
Michael Tobis
Guest
Posts: n/a
 
      01-31-2005
With all due respect, I think "so go away if you don't like it" is
excessive, and "so go away if you don't like it and you obviously don't
like it so definitely go away" is more so. The writer is obviously
neither a native speaker of English nor an accomplished user of Python,
so there are two language issues here. Try expressing your reply in
Russian before deciding that "very ugly" means exactly what you think
it does. I think just saying that "experienced Python users have not
found the lack of declarations to be a major hindrance" would have been
more appropriate.

Also, the assertion that "Python has no declarations whatsoever" is no
longer obviously true. In the 2.4 decorator syntax, a decorator line is
not executable, but rather a modifier to a subsequent symbol binding. I
call it a declaration. I found this disappointing in that it seems to
me a violation of "Special cases aren't special enough to break the
rules" but on further reflection it enables consideration of what a
whole slew of declarative constructs could achieve.

To begin with, now that the design constraint of "no declarations" has
been shown to be less than absolute, why not allow for perl-style ('use
strict') declarations? It actually is very useful in the small-script
space (up to a few hundred lines) where the best Perl codes live.

Let me add that I remain unconvinced that a language cannot combine the
best features of Python with very high performance, which is ultimately
what I want. It seems to me that such a language (possibly Python,
possibly a Python fork, possibly something else) will need substantial
programmer control over references as well as referents.

It seems to me that Python has a weaker case for purity in this regard
now that the dam has been breached with decorator syntax, which is, I
think, a declaration.

Let me conclude with the confession that I'm still on the steep part of
the learning curve (if it ever flattens out at all...). Python has
definitely significantly modified how I think about things, and I
deeply appreciate all the efforts of you veterans. So I say this all
with some trepidation, because I don't want to join Alexander in
inadvertently offending you. And since I presumably missed some intense
flame wars about decorators by only a couple of months, this may be a
real hornet's nest I am poking.

In summary, again with all due respect and gratitude for the
spectacularly excellent product that Python is today, I wonder *why*
this strong aversion to declarative statements, and *whether* decorator
syntax constitutes a violation of it. I'd appreciate any responses or
links.

--
mt

 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      01-31-2005
Michael Tobis wrote:

> Also, the assertion that "Python has no declarations whatsoever" is no
> longer obviously true. In the 2.4 decorator syntax, a decorator line is
> not executable


that's a nice theory, but since the decorator line is executed by the inter-
preter, it's a little weak.

</F>



 
Reply With Quote
 
Michael Tobis
Guest
Posts: n/a
 
      01-31-2005
> that's a nice theory, but since the decorator line is executed by the
inter-
> preter, it's a little weak.


Well, uh, who else would process it?

"use strict' and 'my epsilon' in perl are executed by the perl
interpreter as well, but they have a declarative flavor.

A decorator is a modifier to a subsequent binding, and it modifies the
reference and not the referent. So how is it anythng but declarative?
--
mt

 
Reply With Quote
 
Fredrik Lundh
Guest
Posts: n/a
 
      01-31-2005
Michael Tobis wrote:

>> that's a nice theory, but since the decorator line is executed by the
>> interpreter, it's a little weak.

>
> Well, uh, who else would process it?


the compiler.

from __future__ is a declaration. @expression is an executable statement.

</F>



 
Reply With Quote
 
DogWalker
Guest
Posts: n/a
 
      01-31-2005
"EP" <> said:

>> ------------Original Message------------
>> From: rg (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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
maxplusII error: a deferred constant declaration without a full declaration is not supported Noah VHDL 5 04-07-2006 02:34 PM
Variable declaration taken as a function pointer declaration Bolin C++ 4 12-02-2005 05:28 PM
variable definiton / variable declaration baumann@pan C Programming 3 05-11-2005 01:01 PM
Function declaration in class declaration Ovidesvideo C++ 4 12-10-2004 06:36 PM
Intel C++ 8.0 : declaration hides declaration Alex Vinokur C++ 4 04-05-2004 09:49 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57