Static typing, Python, D, DbC

Bearophile bearophileHUGS at lycos.com
Sat Sep 11 10:33:29 EDT 2010


I write some Python code almost every day, but lately I am using a lot
the D language too. After using D for about three years I now know
some of it, but when I need to write short (< about 1000 lines)
*correct* programs, Python is still the more productive for me.

Static typing, and the usage of transitive const of D are a strange
thing. It seems obvious that they lead to safer code, but on the other
hand experimentally I have seen that my short Python programs are less
buggy than equivalent D ones.

Static typing looks safer, but D offers many low level features, and
generally it contains many more traps or ways to shoot your own foot,
that they more than compensate for the "lack of safety" coming from
Python dynamic typing, even when you don't use those low level
features.

Maybe for large (> about 100_000 lines) programs D may come out to be
less bug-prone than Python (thanks to the better data hiding and
static typing), but I am not sure of this at all...

Static typing also has a significant costs. When you write D2 code
often something doesn't work because of some transitive const or
immutable (or just because of the large number of bugs that need to be
fixed still in the D compiler). So here you pay some cost as debugging
time (or time to avoid those problems). And there is a mental cost
too, because you need to keep part of your attention on those const-
related things instead of your algorithms, etc.

----------------

Lately while I program with Python one of the D features that I most
miss is a built-in Design By Contract (see PEP 316), because it avoids
(or helps me to quickly find and fix) many bugs. In my opinion DbC is
also very good used with doctests.

You may implement a poor's man DbC in Python like this:

class Foo:
    def _invariant(self):
        assert ...
        assert ...
        return True

    def bar(self, ...):
        assert self._invariant()
        ...
        res = ...
        assert self._invariant()
        return res

But this missed several useful features of DbC.


>From the D standard library, I have also appreciated a lazy string
split (something like a str.xplit()). In some situations it reduces
memory waste and increases code performance.

----------------

I have installed this, on a Windows Vista OS:
http://www.python.org/ftp/python/2.7/python-2.7.msi

But I have had two problems, the 'random' module was empty, and it
didn't import the new division from the future, so I've had to remove
it and reinstall 2.6.6. Is this just a problem of mine?

Bye,
bearophile



More information about the Python-list mailing list