Re: signature analysis ( was: getargs enhancement )

Jack Jansen (Jack.Jansen@cwi.nl)
Wed, 09 Mar 1994 10:59:45 +0100

Recently, "Steven D. Majewski" <sdm7g@elvis.med.virginia.edu> said:
>
> Many long threads ago, when discussing type checking of arguments,
> someone ( Guido? ) mentioned the possibility ( or was it just the
> desirability) of adding some type of signature analysis to python.
>
> ...
>
> def f( str:type(str)==type(''), seq:hasattr(seq,'__getitem__') ):
> def g( n:type(n) in ( type(0), type(0.0), type(0L) ) or hasattr(n,'__div__')
> ):
> def h( n: 0 <= n <= 9, seq:len(seq)<=9 ):
>

I would like type checking only if it buys me something. The main
thing it could get me, maybe, is more efficient implementation. One of
the reasons for writing a module in C at the moment is efficiency. For
instance, my midi file parser in python is nice and elegant, but too
slow. It would be really nice if I could give python some hints along
the lines of 'this argument will always be a string' and have it
generate more efficient code.

Your function f() is currently as easily coded as
def f(str, seq):
if type(str) <> type('') or not hasattr(seq, '__getitem__'):
raise ArgumentError
and if I really care about error checking I'll write out the second
form as easily as the first. If the syntax were more concise, though,
I could see myself using it more often (argument checking??? whoooaaaaa:-).

--
Jack Jansen        | If I can't dance I don't want to be part of
Jack.Jansen@cwi.nl | your revolution             -- Emma Goldman
uunet!cwi.nl!jack    G=Jack;S=Jansen;O=cwi;PRMD=surf;ADMD=400net;C=nl