pre/post-condition support? 
Author Message
 pre/post-condition support?

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.

Thanks,

Greg

p.s. I'm aware this can be done by hand by putting assertions at the start
and end of methods.  However, that doesn't automatically work right when
methods have multiple exit points (return statements, exceptions that
callers are expected to catch, etc.), and isn't respected by inheritance.



Thu, 16 May 2002 03:00:00 GMT  
 pre/post-condition support?

Quote:

> 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



Fri, 17 May 2002 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Pre-condition vs. Post-condition

2. Pre-condition vs. Post-condition

3. Pre-condition vs Post-condition

4. "BIG E vs little e", was pre-condition vs post-condition

5. pre-condition vs post-condition

6. Where to implement Pre and Post Conditions?

7. Pre- and post-conditions (was Commenting code)

8. "Destructors" and dynamic pre/post-conditions

9. Pre/Post Conditions

10. question about pre- & post-conditions

11. percursor and pre- and post-condition inheritance

12. Pre- and post-conditions under multiple inheritance

 

 
Powered by phpBB® Forum Software