declaration of variables?
Jp Calderone
exarkun at intarweb.us
Sun Feb 16 14:39:35 EST 2003
On Sun, Feb 16, 2003 at 02:55:48PM +0100, Andr? Jonsson wrote:
> Jp Calderone wrote:
> > "enlightened" language? Hrm. Dynamic, sure... OO, okay... functional,
> >sometimes! But "enlightened"?
>
> :-) I mostly meant that Python seems more "aware" than most other
> languages of usability and readability. Do those not count?
>
> > Chances are, if you have this kind of error in your program, there will
> > be
> >an error, a NameError later on when you try to use "value" and it isn't
> >bound. Compile time is almost always essentially the same as runtime, so
> >you get the information at about the same time. Even better, if you have
> >unit tests that cover this code path, you find out before you even run your
> >actual program.
> >
> > Could the problem be avoided with variable declarations? Yep. Would I
> >trade the flexibility of the current system for a small improvement in
> >compile-time checking of my code? Nah, I don't think so.
>
> Flexibility? Please explain. To me this just seem error-prone, and a pain
> in the b*tt.
>
I'd think it was obvious. Right now, I can bind any name any time I like.
With declarations, any time I wanted a new variable, I'd have to type an
extra keyword somewhere. Any time I decided I didn't need a variable any
more, I'd have to go find where it was declared and remove it. If you think
this isn't a significant hurdle, I recommend you go read some of the larger
C projects out there. In many of them, you'll find all kinds of accrued
variable cruft - variables people declared, used, and then discarded, but
failed to remove the declarations. This reduces program readability, and
would presumably slow down program execution in Python as well.
> Also, it may not even be possible to detect by a unit-test, because there
> may not be a syntactical error or such that would cause a run-time error,
> it just "behaves" strangely (see my example of a _very_ simple case). In a
> larger program/project this could get kinda complex. But I guess that kind
> of stuff aren't supposed to be done in Python then...
>
It is *always* possible to detect something with a unit test. Your
statement about "syntactical errors" suggests that you haven't had much
experience with unit tests. Let me give you an example.
def SimpleAdder:
value = 0
def __init__(self, value = 0):
self.value = value
def setValue(self, value):
self.valeu = value
def doAdd(self, toWhat):
return self.value + toWhat
import unittest
def SimpleAdderTestCase(unittest.TestCase):
def setUp(self):
self.adder = SimpleAdder()
def testBasicAddition(self):
self.assertEquals(self.adder.doAdd(5), 0 + 5)
def testChangeValue(self):
self.adder.setValue(5)
self.assertEquals(self.adder.doAdd(5), 5 + 5)
This trivial test case should show just how *any* kind of errors can be
caught with a unit test, not just syntactic ones.
Jp
--
up 8 days, 0:28, 3 users, load average: 0.04, 0.04, 0.00
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20030216/5d336224/attachment.sig>
More information about the Python-list
mailing list