Pre/Postconditions with decorators

Stephen Thorne stephen.thorne at
Thu Jan 6 18:02:40 EST 2005

On 6 Jan 2005 13:33:42 -0800, Rittersporn <rittersporn at> wrote:
> @condition("number>0 and number<2","result>=0")
> def sqrt(number):
>         import math
>         return math.sqrt(number)
> @condition("list(seq) is not None","sum(seq)==result")
> def my_sum(seq):
>         tmp=0
>         for element in seq:
>                 tmp+=element
>         return tmp
> print sqrt(1.2)
> print my_sum([1,2,3])

I think it would be nicer to have the pre and post conditions being compilable.

@condition((list(seq) is not None for seq in args), (sum(seq)==result
for ((seq,), result) in (args, result))

or something silly like that.

Personally, I'd prefer this:
@precondition(list(seq) is not None for seq in args)
@postcondition(sum(seq)==result for ((seq,), result) in (args, result))

(this is, of course, using the framehack lambda replacement presented
in a thread about a week ago).



More information about the Python-list mailing list