Static typing, Python, D, DbC

Lie Ryan lie.1296 at gmail.com
Sat Sep 11 18:36:28 CEST 2010


On 09/12/10 00:33, Bearophile wrote:

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

I would do it like this:

from DbC import DbC
class Foo(__metaclass__=DbC):
    def __invariant(self):
        ... automatically asserted for all methods ...
    def __precond(self):
        ... automatically asserted for all methods ...
    def __postcond(self):
        ... automatically asserted for all methods ...

    @precond(attr=value) # asserts self.attr==value
    @postcond(func) # a function for more complex assertions
    def bar(self, ...):
        ... clean, uncluttered code ...

and set DbC.uninstall() to uninstall all precond/postcond/invariants at
runtime without any additional overhead. These are all definitely
possible with metaclasses and decorators.

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

Care to open an issue at the tracker? Considering that many Python 3
builtins is now lazy, there might be a chance this is an oversight, or
there might be a reason why string.split is not lazy.



More information about the Python-list mailing list