[Python-Dev] Re: Python-Dev Digest, Vol 9, Issue 17

Isaac ishnigarrab at earthlink.net
Mon Apr 5 15:37:32 EDT 2004


>From: Guido van Rossum <guido at python.org>
>
[...]

>I also note that accepting decorator-before-colon now would make it
>harder to come up with a decent syntax for declaring the return type,
>which I still want to do in some future version of Python with
>optional (!) static typing.  But not impossible -- there's enough
>punctuation available besides '[' and ':'.
>
[...]

>--Guido van Rossum (home page: http://www.python.org/~guido/)
>

One thing I was thinking about (aloud on #python) was a validator 
syntax, something resembling:

    def is_int(i):
        return isinstance(number, int):

    def is_hashable(h):
        try:
            hash(h)
            return True
        except TypeError:
            return False # or raise exception

    def func(is_int : number, hashable : key):
        # code...

which translates to:

    def func(number, key):
        if isinstance(number, int):
            raise ValueError, "first argument must be int"

        try:
            hash(key)
        except TypeError:
            raise ValueError, "second argument must be hashable"

        # code...
         

I don't know if it would be best to go the boolean or the exception 
route. Either way, you get the point. This way python retains it's 
dynamicy but allows for type checking as well as other form of 
validation (i.e. range checking, etc). Of course, the validator and the 
colon are optional

    'def' NAME '(' [validator ':'] v_name ',' ... ')' ':' suite

or some crap like that, I'm not a language lawyer, but you get the idea. 
And for people concerned with wasting the time with 10 function calls 
per function call, there could be built-in validators (like is_int, or 
whatever you want to call it) which of course are written an C, and 
maybe some shortcutting could be done in the interpreter (or in psyco, 
maybe) which would allow is_int to be called with out all the pythonic 
function calling crap (I'm not sure exactly how this all works...). 
Something like that.

Also as an extension of that, there can a

    def ret_type : func(arg, arg, ...): pass

kinda thing... although the colon might confuse things, there idea is 
there, just needs to be hammered out.

Anyways, just some random thoughts. I'd be happy to work with some 
people to make a PEP out of this, if it gains any traction, just e-mail me.

Isaac



More information about the Python-Dev mailing list