Static Typing in Python

Sean Ross sross at connectmail.carleton.ca
Sat Mar 13 12:47:43 EST 2004


"Gonçalo Rodrigues" <op73418 at mail.telepac.pt> wrote in message
news:01b650lrd53rbab3uis6dlqv38u52pk0e8 at 4ax.com...
[snip]
> I don't know a thing about perl, but by your description, strict
> pragma is to solve a problem *in perl that does not exist in Python*.
> If you use a variable before declaring (I'm using your terminology --
> it's not the most correct in the Python world, though) Python just
> raises an exception, e.g.
>
> >>> #No var declared... exception raised:
> >>> print var
[snip]

There's another class of problem:

# trivial example based on http://io.kicks-ass.net/LocalVariables
class A:
    def __init__(self):
        self.area = None
    def square(self, side):
        self.arae = side * side     # spelling mistake creates new attribute
    def circle(self, radius):
        self.area = 3.14 * radius * radius

a = A()
a.circle(1)
print a.area  # 3.14 -> ok
a.square(4)
print a.area  # 3.14 -> unintended


Having never used pychecker, I can't say for certain, but I expect it will
catch problems of this sort, and, as someone else suggested, unit tests
should also nip this in the bud. Still, while I don't advocate it, I would
not
be adverse to *optionally* letting the language impose some form of
distinction between variable creation and variable update - which is what
"use strict" allows. Whether you use it or not is left up to your own tastes
and/or discretion - you're not forced to declare your variables if you don't
want to (although you will be forcing others to read it, if you do). But, if
you do want to, you can, and the language will help enforce this choice.

So, hypothetically, you could introduce an optional let statement:

use strict  # or, from __future__ import let, or nag, or whatever
class A:
    def __init__(self):
        let self.area = None         # uses 'let' for variable creation
    def square(self, side):
        self.arae = side * side     # Exception, undeclared variable "arae"
    def circle(self, radius):
        self.area = 3.14 * radius * radius

Now the language would catch this error in your code for you - no need
to run outside programs (pychecker, your test suite) to find something
so trivial. You'd have a whole lot of 'let's cluttering up your code - but,
hey,
you asked for it ... (of course, I didn't ask to have to read it - but
that's beside
the point - or, is that the point?)


Still, there are issues. For instance:

use strict
class B:
    def __init__(self, **kwds):
         self.__dict__.update(kwds)  # I "know" one key/value will be
"b:'B'"
    def baz(self):
        print self.b    # so, I'll use it here.
                              # Not best practice, perhaps, but whatever ...
b = B(b="B")
b.baz()  # Exception?


So, here we're adding attributes dynamically, one of which is/will be
self.b.
But, self.b was not explicitly created (there's no "let self.b = 'B'"). Can
I
use self.b? I wouldn't expect so. I imagine the answer in this case would be
to not use strict.

There are probably other problems besides restricting dynamism and adding
clutter, but I'm sure others will be along shortly to point those out.

Sean







More information about the Python-list mailing list