根据命名约定检查函数参数类型

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了根据命名约定检查函数参数类型相关的知识,希望对你有一定的参考价值。

not an answer for everything, but perhaps for basic types
  1. from types import *
  2.  
  3. def bfnCheckArgTypes(*args):
  4. sErr = ""
  5. # loops through: ('sString', 'tupTuple', 'iInteger', 'lstList')
  6. for iInd, sArgName in enumerate(args[0].func_code.co_varnames):
  7. if sArgName.startswith("tup") and not type(args[iInd+1]) is TupleType:
  8. sErr += " arg '%s' is not of type 'tuple'" % sArgName
  9. elif sArgName.startswith("lst") and not type(args[iInd+1]) is ListType:
  10. sErr += " arg '%s' is not of type 'list'" % sArgName
  11. elif sArgName.startswith("i") and not type(args[iInd+1]) is IntType:
  12. sErr += " arg '%s' is not of type 'int'" % sArgName
  13. elif sArgName.startswith("s") and not type(args[iInd+1]) is StringType:
  14. sErr += " arg '%s' is not of type 'str'" % sArgName
  15. if not sErr == "":
  16. print "Function '%s' has argument errors: %s" % (args[0].__name__, sErr)
  17. return False
  18. return True
  19.  
  20.  
  21. def fnTestMe(sString, tupTuple, iInteger, lstList):
  22. assert bfnCheckArgTypes(fnTestMe, sString, tupTuple, iInteger, lstList)
  23.  
  24.  
  25. # Test the code with these:
  26.  
  27. fnTestMe("string-OK", ("tuple","OK"), 55, ["list","OK"]) # all OK
  28.  
  29. #fnTestMe("string-OK", "notTuple", 55, "notList") # 2nd and 4th fail
  30.  
  31. fnTestMe(55, ("tuple","OK"), "string-NOK", ["list","OK"]) # 1st and 3rd fail

以上是关于根据命名约定检查函数参数类型的主要内容,如果未能解决你的问题,请参考以下文章

试图根据约定命名委托方法,Xcode“期待一个类型”

PostgreSQL中的函数中是不是有命名参数的约定

Typescript(真的)是不是遵循泛型中参数化类型(T、U、V、W)的命名约定?

google风格C/C++编程规范 --- 命名约定

google风格C/C++编程规范 --- 命名约定

Swift6-函数