This article mainly introduces the simple method of realizing parameter type checking in Python, this article explains using the adorner to implement the parameter type checking and gives the code example, the friend who needs can refer to the following
Python is a weakly typed language, and many of the friends who turn from C + + are not very comfortable at first. For example, when declaring a function, you cannot specify the type of the parameter. The analogy with C is that all parameters are void* type!void type cast is widely considered a bad habit in C + + and will not be used unless it is a last resort.
Python naturally does not have the type cast one says, because it is a dynamic language. First, all objects inherit from object, and second, it has a strong introspection, and if a method that does not exist is invoked, an exception is thrown. Most of the cases, we do not need to do the parameter type of the Bolt check, except for some special circumstances. For example, a function accepts a str type, the result is Unicode when the actual call is made, and no code is covered during the test, and the problem is more serious. The solution is also simple, with the help of Python introspection, it is easy to tell the type of the parameter. But it would be cumbersome to write and check the code everywhere, not to mention the actual value it brings. A good workaround is to use an adorner.
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 This is the only of the "a" of the--------------- TD style= "BORDER:0;PADDING:0;" > ' >>> NONE, MEDIUM, strong = 0, 1, 2 >>> >>> @accepts (int, int, int) ... def average (x, Y, Z): ... return (x + y + z)/2 ... >>> average (5.5, 15.0) typewarning: ' Average ' method accepts (int, int, T), but was given (float, int, float) 15.25 ' Def accepts (*types, **kw): "" "Function decorator. Checks that inputs given to decorated function are of the expected type. Parameters:types-The expected types of the inputs to the decorated function. Must specify type for each parameter. KW-Optional specification of ' Debug ' level (this is the valid keyword argument, no other should to be given). debug = (0 | 1 | 2) "" "If not kw: # default Level:medium debug = 1 Else:debug = kw[' Debug '] try:def decorator (f): Def NEWF (*args): if debug = = 0:return F (*args) assert Len (arg s) = = Len (types) argtypes = tuple (map (type, args)) if argtypes!= types:msg = info (f.__name__, types, argtypes, 0) if Deb UG = = 1:print >> sys.stderr, ' typewarning: ', msg elif debug = = 2:raise TypeError, msg return F (*args) Newf.__name __ = f.__name__ return NEWF return decorator except Keyerror, Key:raise keyerror, key + ' not a valid keyword ' argument ' Except TypeError, Msg:raise TypeError, msg DEF info (fname, expected, actual, flag): "" "Convenience function Retu RNs nicely formatted error/warning MSG. "" "format = Lambda types: ', '. Join ([str (t). Split (" ") [1] for T in types]) expect ed, actual = format (expected), format (actual) msg = "'%s ' method"% fname + ("accepts", "returns") [flag] + "(%s), but" % expected + (' was given ', ' result is ') [flag] + ' (%s) '% actual return msg |
Essentially, this is also a run-time check, but the effect is good.