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 Spinner选定值未上载到firebase数据库   java如何通过bukkit中的配置添加消息?   java在SharedReference中保存列表的泛型类型   javascript Java小程序未定义   swt在Java中构建控制台应用程序   java OAuth同意屏幕没有突然显示,没有错误?   java webview选择文件安卓不工作   java Spring boot JPA如何从同一连接添加多个数据库?   java JDBC DB2驱动程序计时器线程处于阻塞状态   java我在vscode中运行flatter时看到一个错误在phone中运行应用程序somone能否帮助我plz   Java:生成JSON:如何避免生成不完整的JSON   java Date compareTo()方法始终返回1   当使用Junit runner运行测试时,java Spring自动连线失败   java Android/Sockets如何将信息从主UI线程发送到socket线程?   java Android内存声明全局变量   java如何在JBoss中拥有多个具有相同JNDI名称的数据源?   python在Java中嵌入CPython时,为什么会挂起?   java如何提供深度模拟对象?   java“find:smallint,expected:integer”Hibernate对informix数据库的验证在短时间内失败