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

Josiah Carlson jcarlson at uci.edu
Mon Apr 5 18:27:40 EDT 2004


> 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,

Everything you want with "validators" is going to be available with
decorators from PEP 318.

 - Josiah




More information about the Python-Dev mailing list