473,715 Members | 2,235 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

variable declaration

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, za**@bk.ru
Jul 18 '05 #1
83 6497
EP
> ------------Original Message------------
From: Al************* ********@p131.f 3.....fido net.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 mistakein 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.

Jul 18 '05 #2
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

Jul 18 '05 #3
Alexander Zatvornitskiy
<Al************ *********@p131. f3.n5025.z2.fid onet.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?th read=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
Jul 18 '05 #4
Alex Martelli wrote:
Alexander Zatvornitskiy
<Al************ *********@p131. f3.n5025.z2.fid onet.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
Jul 18 '05 #5
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 "experience d 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

Jul 18 '05 #6
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>

Jul 18 '05 #7
> 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

Jul 18 '05 #8
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>

Jul 18 '05 #9
"EP" <EP@zomething.c om> said:
------------Original Message------------
From: Al************* ********@p131.f 3.....fido net.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

Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
2668
by: YGeek | last post by:
Is there any difference between declaring a variable at the top of a method versus in the code of the method? Is there a performance impact for either choice? What about if the method will return before the variable is used? I'm trying to get an idea of whether the .NET compilers for VB.NET and C# will move all variable declaration to the beginning of a method or whether they will allocate the memory as it is needed by the method to...
12
3098
by: Michael B Allen | last post by:
Which style of local variable declaration do you prefer; put everything at the top of a function or only within the block in which it is used? For example; void fn(struct foo *f, int bar) { struct abc d; int i;
7
7714
by: seamoon | last post by:
Hi, I'm doing a simple compiler with C as a target language. My language uses the possibility to declare variables anywhere in a block with scope to the end of the block. As I remembered it this would be easily translated to C, but it seems variable declaration is only possible in the beginning of a block in C. Any suggestions how to get around this?
5
2422
by: widmont | last post by:
Hello, I would like to know the difference between two variable declaration ways: int point(0); and int point=0;
21
2830
by: Kannan | last post by:
Its been a while I have done pure C programming (I was coding in C++). Is the following function valid according to standard C? int main(int argc, char *argv) { int x; x = 9; printf("Value of x is: %d\n", x);
9
6798
by: Denis Petronenko | last post by:
i can write something like this int foo() { return 100; } if( int x=foo() ) { .... }else{
14
13055
by: subramanian100in | last post by:
Consider the following program: #include <iostream> using namespace std; int main() { int i;
15
14909
by: vaib | last post by:
hi to all.i'd like to know the actual difference between variable declaration and definition.it would be very helpful if anyone out there wud help me out with this thing.i'm writing here after here after a long time since people here also helped me out with my lexer. thanking in anticipation.
1
11861
NeoPa
by: NeoPa | last post by:
Problem Description : In VBA there is an option to enforce declaration of variables in your code. With this set, any reference to a variable that has not been previously declared (Dim; Private; Public; Global; etc) will cause an error, either at compile time or when attempting to execute the procedure it's referred to from within. With this unset, any unregognised references will be treated as a previously unknown and unset variable of type...
0
8718
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9332
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9195
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9044
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7969
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5966
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4738
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2537
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2118
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.