[Python-Dev]
Validators (was Re: Python-Dev Digest, Vol 9, Issue 17)
Isaac
ishnigarrab at earthlink.net
Mon Apr 5 21:30:32 EDT 2004
Guido:
>Please note that you seem to be using the syntax ``type: variable''.
>We've settled on using ``variable: type'' instead. (How to express
>types is quite a different story...)
>
I haven't been following the thread on the subject much at all, I've
just heard some rumbling about introducing static typing into python,
and frankly I'm a bit wary about the whole thing. I was just thinking
that in keeping with python's dynamic nature that something more like a
generalized way of validating the _nature_ of something as opposed to
it's underlying *type* would be more appropriate. It's just that it
rings more pythonic to me (not that I have in any way graduated to the
rank of Pythonist to make such judgments). As for the order of things,
it's really not that important, "name: validator" would be just as
useful. The only problem would be that
def func(name: int): pass
works in terms of type checking, as in
def func(name):
if not isinstance(name, int):
raise TypeError
where as translating it as a validator:
def func(name):
int(name)
doesn't make as much sense, int would have to be overloaded or something
like is_int would have to be implemented.
But the advantages of validators would be numerous, IMO:
* custom validators (equivalent to custom 'types' like in_range, or
iterable)
* still allows for the same functionality as typing systems while
retaining dynamic capabilities.
* possibly inline conversions (interface mutators, adaptors, I wonder
if twisted might like something like this).
i.e.:
def add(a: int, b: int): return a+b
add('45', 5) => 50
if it were established that validators raise exceptions if the input is
invalid, and return the value (possibly modified) if valid. Similar to
implicit upcasting, i.e.:
class MyInt(int):
def __init__(self, i):
self.i = i
def add(self, other):
self.i += other.i
return self
def add(a: MyInt, b: MyInt):
return a.add(b)
add(4, 4) => MyInt(8)
Again, I'm mostly just thinking aloud, hoping to be productive.
Isaac
More information about the Python-Dev
mailing list