Pre/Postconditions with decorators
Robert Brewer
fumanchu at amor.org
Thu Jan 6 18:38:26 EST 2005
Stephen Thorne wrote:
> On 6 Jan 2005 13:33:42 -0800, Rittersporn
> <rittersporn at gmail.com> 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.
Me too.
> @condition((list(seq) is not None for seq in args), (sum(seq)==result
> for ((seq,), result) in (args, result))
>
> or something silly like that.
Ian Bicking was just talking about @require decorators:
http://blog.ianbicking.org/already-under-our-noses.html
@require(int, int)
def gcd(a, b):
...
If we made a "checker" module for such things in the stdlib, we could
write most of that:
from checker import *
@args((list, itemsnotNone, ))
@returns((any, ))
def my_sum(seq):
tmp=0
for element in seq:
tmp+=element
return tmp
Robert Brewer
MIS
Amor Ministries
fumanchu at amor.org
More information about the Python-list
mailing list