pre/post-condition support?
Martin von Loewis
loewis at informatik.hu-berlin.de
Mon Nov 29 14:45:28 EST 1999
"Greg Wilson" <gvwilson at nevex.com> writes:
> Has anyone experimented with Python extensions to support programming by
> contract (pre/post conditions on methods + state assertions that are
> respected by inheritance)? If so, I'd welcome pointers.
As for pre/post conditions and state assertions: with Python 1.5.x,
there is the assert statement:
class Data:
def __init__(self):self.items = {}
def add(self,key,value):
assert (not self.items.has_key(key))
self.items[key] = value
d = Data()
d.add("milk","white")
d.add("milk","white")
As for respecting inheritance: I guess approaches taken in other
languages work in Python as well:
class Base:
def method(self,params):
assert preconditions
result = self.method_implementation(self,params)
assert postconditions
return result
You can probably apply a number of reflection-based tricks in Python;
I doubt that this would make your code more understandable.
Regards,
Martin
More information about the Python-list
mailing list