Proposal: runtime validation statement

Ville Vainio ville at spammers.com
Mon Jul 12 11:22:35 EDT 2004


>>>>> "Paul" == Paul Rubin <http://phr.cx@NOSPAM.invalid> writes:

    Paul> takes care of it, of course, so it's slightly redundant to
    Paul> add a special statement like

    Paul> validate x >= 0, (x, "must not be negative")

    Paul> which works exactly like assert but raises a different
    Paul> exception and is never optimized away.  But the same can be
    Paul> said of the print statement (use sys.stdout.write or a print
    Paul> function instead) and for that matter the addition operator
    Paul> (use "x - (-y)" instead of x+y), the bool type (use 1 and 0
    Paul> instead of True and False), etc.

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.

    Paul> steering what the common idioms should be.  Using a
    Paul> user-defined function to check input means a couple more
    Paul> program-specific things to remember (the function itself and
    Paul> the exception class it raises), clutters up the code, etc.
    Paul> And so I've come to feel that a "validate" statement (maybe
    Paul> with some different keyword) like the above is in the
    Paul> Pythonic spirit and should be considered for some
    Paul> forthcoming release.

Any specific reason not to make it a builtin function instead of
statement? 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))

(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)

Before something like that goes "official", help tools and IDEs can't
use the type information.

-- 
Ville Vainio   http://tinyurl.com/2prnb



More information about the Python-list mailing list