decorator,它允许您在函数或方法中指定参数的有效类型。

RequireType的Python项目详细描述


什么

decorator,它允许您在 功能或方法。

如果调用与所需类型不匹配,则将失败 TypeError异常。

为什么?

在某些情况下(todo:添加一些示例),您希望/需要指定 要使用的特定类型,并且由于python没有参数的类型检查 这是有用的地方。

<> P>所有我发现的解决方案都有问题或者没有 我想要的功能,所以我自己做了。

如何使用?

首先,安装RequireType包,可以使用pip
PIP安装要求类型

在代码中,您只需要导入并使用requiredecorator。

示例:

from requiretype import require

@require(name=str, age=(int, float, long))
def greet_person(name, age):
    print "Hello {0} ({1})".format(name, age)

>>> greet_person("John", 42)
Hello John (42)

>>> greet_person("John", "Doe")
[...traceback...]
TypeError: Doe is not a valid type.
Valid types: <type 'int'>, <type 'float'>, <type 'long'>

>>> greet_person(42, 43)
[...traceback...]
TypeError: 42 is not a <type 'str'> type

一些注释

这里有一些关于这个图书馆的细节。大多数是在一个或 帮助您强制/检查/要求类型和imo的更多包不是 很好的东西激励我写这篇文章。

要求类型:

  • does not modify ^{tt5}$ or ^{tt6}$
  • does not move arguments from one place to another
  • support both ^{tt5}$ and ^{tt6}$
  • allows you to use all the python 2.x supported parameters usage (if not, please report a bug)
  • use named arguments type definition
  • allows you to enforce a subset of all the available arguments
  • raise a standard ^{tt1}$ if the arguments type are wrong

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java无法使用Intellij Idea执行Xpath   java为什么springbootstarterdatajpa 2.5.0不能因“未找到表”而初始化数据?   google maps Geoppoint类中的java丢失精度   java fb graph对象中有哪些属性可用?   实现JNI时指针类型的java错误   java使用管理目录API将一个组织单元移动到另一个组织单元?   java局部变量gcd可能尚未初始化   java示例代码未按预期执行   使用持久性的JPA的数据库连接位于何处。xml?   java从AJAX成功函数中检索ArrayList元素   java中的持久文件验证   java编码简约消息的最佳方式   tapestry用java生成站点地图并使其公开   Java文档中使用的哈希集约定   java试图在工作线程上调用join   java有没有时间函数来记录一个方法完成一项工作所需的时间?   如何让计算器在按下等号后接受新数字?JAVA