variable declaration

Alex Martelli aleaxit at
Mon Jan 31 07:46:38 EST 2005

Alexander Zatvornitskiy
<Alexander_Zatvornitskiy at> 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:

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


More information about the Python-list mailing list