相关疑难解决方法(0)

什么是Python 3.5中的类型提示

__CODE__据说其中一个被谈到的功能__CODE__.

的例子__CODE__本中提到的文章同时也提到使用类型负责任提示.有人可以解释更多关于它以及什么时候应该使用它什么时候不使用?

python type-hinting python-3.x python-3.5

216
推荐指数
3
解决办法
9万
查看次数

什么是Python 3.6中的变量注释?

Python 3.6即将发布.PEP 494 - Python 3.6发布时间表提到12月底,所以我通过Python 3.6中的新功能看到他们提到了变量注释:

PEP 484引入了函数参数类型注释的标准,即类型提示.此PEP为Python添加语法以注释变量类型,包括类变量和实例变量:

primes: List[int] = []

captain: str  # Note: no initial value!

class Starship:
     stats: Dict[str, int] = {}
Run Code Online (Sandbox Code Playgroud)

与函数注释一样,Python解释器不会将任何特定含义附加到变量注释,只将它们存储在__annotations__类或模块的特殊属性中.与静态类型语言中的变量声明相比,注释语法的目标是提供一种通过抽象语法树和__annotations__属性为第三方工具和库指定结构化类型元数据的简便方法.

因此,根据我的阅读,它们是来自Python 3.5的类型提示的一部分,在Python 3.5中的什么是类型提示中有所描述.

我按照captain: strclass Starship示例,但不确定最后一个:如何primes: List[int] = []解释?它是否定义了一个只允许整数的空列表?

python annotations type-hinting python-3.x python-3.6

62
推荐指数
2
解决办法
4万
查看次数

如何在python中强制静态类型

因为静态类型在python 3.6中可用.

是否可以强制python项目或python文件集的静态类型?

python static-typing typing python-3.6

11
推荐指数
2
解决办法
5933
查看次数

mypy麻烦列表对象的继承

Python 3.6.5和mypy 0.600

我写了代码:

from typing import List


class Animal():
    pass


class Dog(Animal):
    def __init__(self) -> None:
        super()

    def bark(self) -> None:
        pass


class Cat(Animal):
    def __init__(self) -> None:
        super()

    def meow(self) -> None:
        pass


arr1: List[Dog] = [Dog(), Dog()]
arr2: List[Animal] = [Dog(), Dog()]

# error: Incompatible types in assignment (expression has type "List[Dog]", variable has type "List[Animal]")
arr3: List[Animal] = arr1
Run Code Online (Sandbox Code Playgroud)

我不明白,为什么我有一个错误“赋值中的不兼容类型”和变量“ arr3”。狗是从动物继承的类。例如,变量'arr2'没有错误。

python typing python-3.x mypy

6
推荐指数
2
解决办法
482
查看次数

Python 3.5+ 中类型提示的动态检查

typing模块在 Python 3.5+ 中实现类型提示。然而,这并没有强制执行,它目前似乎只是为了静态类型检查器(例如 PyCharm )的利益mypy存在。我希望它能成为鸭子打字的可行替代方案。

问题:有没有一种方法可以在 Python 3.7+ 中打开动态类型检查,但我在 Google 搜索中没有找到?例如,如果我定义

def greeting(name: str) -> str:
    return name
Run Code Online (Sandbox Code Playgroud)

那么这应该在执行时失败:

greeting([12])
Run Code Online (Sandbox Code Playgroud)

我不介意为此检查付出时间代价,因为出于我的目的,我必须使用assert语句手动实现它,并且类型提示更加简洁和描述性。

更新:下面的评论者指出typen包将为我动态强制执行类型提示。因此,这是一个肯定的答案,它将更新旧问题的答案,该问题的范围仅限于 Python 3.6,并且答案是否定的。我已经验证规范typen示例是否按预期工作:

from typen import enforce_type_hints

@enforce_type_hints
def halve_integer(a: int) -> float:
    return a / 2

halve_integer(5)  # 2.5
halve_integer(5.0)  # ParameterTypeError
Run Code Online (Sandbox Code Playgroud)

唯一的缺点是每个函数都需要进行修饰才能获得行为,而不是有一个开关来打开所有功能。

更新 2:下面的回答还指出pydantic也解决了这个问题。这是两个正解。然而,pydantic似乎更适合数据建模,并且对其验证装饰器有一些强烈的警告:

validate_arguments 装饰器处于beta阶段,它已在 v1.5 中临时添加到 pydantic 中。它在未来的版本中可能会发生重大变化,并且其界面直到 v2 …

python duck-typing python-3.x python-typing

6
推荐指数
1
解决办法
3385
查看次数

如何正确使用python类型提示?

我在文档中读到 Python 3.5 版本中存在类型提示我在 ipython 终端中编写了两个函数来测试这对“相同”函数意味着什么。

def dostuff(name: str) -> str:
  print(str.capitalize())

def do_stuff(name):
  print(str.capitalize())
Run Code Online (Sandbox Code Playgroud)

正如预期的那样,调用dostuff('arthur')do_stuff('arthur')都返回 'Arthur'。

但是,调用do_stuff([])dostuff([])都返回错误:

AttributeError: 'list' object has no attribute 'capitalize'
Run Code Online (Sandbox Code Playgroud)

这是有道理的,相同的错误在两者中都是有效的,但是为什么类型提示器/检查器实际上没有声明性地声明argument not of type 'str'或其他什么?

另外,如果您定义如下内容:

def do_stuff(name: str) -> str: 
    return list(name)  
Run Code Online (Sandbox Code Playgroud)

即使函数应该返回一个字符串,解释器甚至不会抱怨我返回的是一个列表而不是一个字符串。

我知道这些都是人为的例子,但我做错了什么吗?

python types type-hinting python-3.x python-typing

1
推荐指数
1
解决办法
3743
查看次数

Python 类型提示与数字

我刚刚遇到了 Python 类型提示的奇怪行为。

>>> def x(y: int):
    return y
>>> d=x('1')
>>> print(type(d))
<class 'str'>
>>> d
'1'
Run Code Online (Sandbox Code Playgroud)

当我将数字作为字符串传递到参数被转换为 int 的函数时,它仍然会生成字符串。我认为这种意想不到的行为的关键在于它不是铸造,而是暗示。但为什么它会这样呢?如果我传递“one”作为参数,它会给我一个 NameError。

python type-hinting python-typing

0
推荐指数
1
解决办法
994
查看次数