gpt4 book ai didi

忽略 Python 类型提示

转载 作者:行者123 更新时间:2023-12-02 20:09:16 28 4
gpt4 key购买 nike

我是 python 新手,正在尝试类型提示,但它们似乎只在某些情况下起作用。它们似乎在属性返回类型上按预期工作,但是当我尝试将整数分配给字符串值(即 self._my_string = 4)时,我没有收到任何问题报告。

class TypeHintTest(object):
_my_string: str

def __init__(self):
self._my_string = 4 # no error

@property
def as_int(self) -> int:
return self._my_string # Error : expected int got str

结果对象包含一个 int 值(如预期)。

我使用的是pyCharm 2018.3.2社区版,解释器是3.6

enter image description here

下面的问题似乎类似,但是将构造函数更改为 def __init__(self) -> None 的解决方案并没有改变任何东西。 Python: All type hints errors in subclass constructure seems ignored

最佳答案

类型提示,顾名思义,就是提示。如果您为变量分配不同的类型,Python 不会引发错误。
然而,Pycharm应该说它需要另一种变量类型。

关于忽略 Python 类型提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54020807/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com