[Python-Dev] Re: Python-Dev Digest, Vol 9, Issue 17 (original) (raw)

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 isint(i): return isinstance(number, int): def ishashable(h): try: hash(h) return True except TypeError: return False # or raise exception def func(isint : 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 ':'] vname ',' ... ')' ':' 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 isint, 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 isint 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 rettype : 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.



More information about the Python-Dev mailing list