any chance for contracts and invariants in Python?

MRAB python at
Thu Feb 14 19:18:53 CET 2013

On 2013-02-14 18:05, Ian Kelly wrote:
> On Thu, Feb 14, 2013 at 10:03 AM, Philipp Hagemeister <phihag at> wrote:
>> So any implementation has to choose one of the following:
>> 1. Ignore invariants and postconditions of inherited classes - defeats
>> the purpose.
>> 2. Only respect definitions in classes and methods in the original
>> definition, which would be unpythonic
>> 3. Only respect the "original" definitions, for some value of original.
>> Simarily, this would break monkey patching.
>> 4. Update all subclasses whenever something changes.
>> 5. Traverse the entire class hierarchy for every method call.
>> Which option should be picked?
> #5, with the expectation that like assertions the entire machinery
> would be turned off when the -O flag is passed, or perhaps even
> requiring a special flag to enable in the first place.  Contracts and
> invariants would only be used in development work, not in production
> code.
Maybe what it needs is a decorator that parses the docstrings, creates
functions to do the checks, and then wraps them around the functions.

More information about the Python-list mailing list