Proposal: runtime validation statement

Paul Rubin http
Mon Jul 12 20:56:33 EDT 2004


Ville Vainio <ville at spammers.com> writes:
> Yes, and I (and many others, I feel) consider print statement a wart
> in the language. Let's not make any more of these... Too bad it's so
> widely used it can't be right out deprecated.

I can sympathize with the notion that print and assert are warts, but
I think they're considered to be important to Python's
newbie-friendliness or something like that.  As such, "validate" ought
to be considered about the same way.

> Any specific reason not to make [validate] a builtin function
> instead of statement?

It's similar enough to assert that for consistency in the language I
think it ought to be done the same way.  But a builtin function would
be ok.

> I wouldn't mind a validation function that could also verify the
> data types of the arguments, which could then be used for code
> completion assistance and type inference... Since we don't know when
> a "real" type declarations happen. Expecting them to hit 2.5 is
> probably a bit too optimistic ;-).
> 
> def a(x,y):
>   validate((x,int), (y,str), x > int(y))

If the compiler is going to rely on something like that, then it
should definitely be a statement, rather than a function that the user
can shadow with his own function that does something completely
different.  But if there's going to be type declarations, they ought
to go into some new construction that cleans up the current scoping
mes at the same time:

    local x:int, y:str          # type declarations
    assert x > int(y)           # compiler can use this as advice

> (validate checks every tuple with isinstance(t[0],t[1]), every arg to
> validate that is "false" in the pythonic falsehoos sense2 fails the
> validation)

This is a little bit bogus: first of all, data validation should be
able to check arbitrary conditions including those that happen to be
tuples.  Second, validation must always be performed and must throw an
exception at runtime, while compiler advice can be optimized away.
So your example is more like "assert" than what I meant by validate.



More information about the Python-list mailing list