功能注释:PEP-3107
我跑过一段代码,展示了Python3的功能注释.这个概念很简单,但我想不出为什么这些在Python3中实现或者对它们有任何好用.也许SO可以启发我吗?
这个怎么运作:
def foo(a: 'x', b: 5 + 6, c: list) -> max(2, 9):
... function body ...
Run Code Online (Sandbox Code Playgroud)
参数后面后面的所有内容都是"注释",后面的信息->是函数返回值的注释.
foo.func_annotations将返回一个字典:
{'a': 'x',
'b': 11,
'c': list,
'return': 9}
Run Code Online (Sandbox Code Playgroud)
有这个有什么意义?
我在python中有一个函数可以返回a bool或a list.有没有办法使用类型提示指定返回类型.
例如,这是正确的方法吗?
def foo(id) -> list or bool:
...
Run Code Online (Sandbox Code Playgroud) 学习python,并有一些基本的疑虑.
我看过变量声明(路径在这里)
class writer:
path = ""
Run Code Online (Sandbox Code Playgroud)
有时,没有明确的声明,但初始化__init__.
def __init__(self, name):
self.name = name
Run Code Online (Sandbox Code Playgroud)
我理解的目的__init__,但是建议在任何其他函数中声明变量.
2.如何创建变量来保存自定义类型?
class writer:
path = "" # string value
customObj = ??
Run Code Online (Sandbox Code Playgroud) Python 3.6即将发布.PEP 494 - Python 3.6发布时间表提到12月底,所以我通过Python 3.6中的新功能看到他们提到了变量注释:
PEP 484引入了函数参数类型注释的标准,即类型提示.此PEP为Python添加语法以注释变量类型,包括类变量和实例变量:
Run Code Online (Sandbox Code Playgroud)primes: List[int] = [] captain: str # Note: no initial value! class Starship: stats: Dict[str, int] = {}与函数注释一样,Python解释器不会将任何特定含义附加到变量注释,只将它们存储在
__annotations__类或模块的特殊属性中.与静态类型语言中的变量声明相比,注释语法的目标是提供一种通过抽象语法树和__annotations__属性为第三方工具和库指定结构化类型元数据的简便方法.
因此,根据我的阅读,它们是来自Python 3.5的类型提示的一部分,在Python 3.5中的什么是类型提示中有所描述.
我按照captain: str和class Starship示例,但不确定最后一个:如何primes: List[int] = []解释?它是否定义了一个只允许整数的空列表?
是否可以使用Python类型提示在Django QuerySet中指定记录类型?有点像QuerySet[SomeModel]?
例如,我们有模型:
class SomeModel(models.Model):
smth = models.IntegerField()
Run Code Online (Sandbox Code Playgroud)
我们想在func中将该模型的QuerySet作为param传递:
def somefunc(rows: QuerySet):
pass
Run Code Online (Sandbox Code Playgroud)
但是如何在QuerySet中指定记录类型,如List[SomeModel]:
def somefunc(rows: List[SomeModel]):
pass
Run Code Online (Sandbox Code Playgroud)
但是使用QuerySet?
我注意到python 3.5和python 3.6增加了很多关于静态类型检查的功能,所以我尝试使用以下代码(在python 3.6中,稳定版本).
from typing import List
a: List[str] = []
a.append('a')
a.append(1)
print(a)
Run Code Online (Sandbox Code Playgroud)
让我感到惊讶的是,python没有给我一个错误或警告,虽然1附加到一个list只应该包含字符串.Pycharm检测到类型错误并给我一个警告,但它并不明显,它没有在输出控制台中显示,我害怕有时我可能会错过它.我想要以下效果:
那可能吗?也许mypy可以做到,但我更喜欢使用python-3.6-style类型检查(比如a: List[str])而不是使用的注释风格(如# type List[str])mypy.而且我很好奇是否在本机python 3.6中有一个切换来实现我上面提到的两点.
在words_pron_dict:str上的冒号是什么意思?我在python 2.7上遇到语法错误.是python 3吗?我怎么用呢?
class TextToSpeech:
CHUNK = 1024
def __init__(self, words_pron_dict:str = 'cmudict-0.7b.txt'):
self._l = {}
self._load_words(words_pron_dict)
Run Code Online (Sandbox Code Playgroud) 我最近被问到这在python中意味着什么:
>>> char : str
我不知道.我以前从未见过.我检查了文档,没有那样的东西.一个人的建议是它是静态类型声明,但文档中也没有任何内容.
有了上述,如果我
>>> type(char)失败了
如果我>>> char : str = 'abc'工作,并且类型(char)的结果是<class: str>.它不能是静态声明,因为我可以>>> char : str = 4和type(char)成为<class: int>.
所以我来这里收集许多SO霸主的智慧.那是什么意思?
我正在寻找一个 Python linter,它可以根据代码中的类型提示检查类型使用情况。
目的是运行单一检查以验证样式、逻辑和类型错误。
我需要在 CI 服务器上运行它,并在开发过程中作为文件观察者。
例如,我需要此代码来输出错误以传递错误的类型参数 -
def double(x: int):
return x * 2
result = double('hello')
Run Code Online (Sandbox Code Playgroud)
我检查了 PyLint 和 flake8 的文档,但找不到任何对类型检查的支持。
使用 PyLint,我还验证了检查上述代码时没有错误。
__init__python中函数的正确类型注释是什么?
class MyClass:
...
Run Code Online (Sandbox Code Playgroud)
以下哪项更有意义?
def __init__(self):
# type: (None) -> None
def __init__(self):
# type: (MyClass) -> MyClass
def __init__(self):
# type: (None) -> MyClass
Run Code Online (Sandbox Code Playgroud)
因为我们通常会实例化为myclass = MyClass(),但__init__函数本身没有返回值.
python ×10
python-3.x ×6
type-hinting ×5
annotations ×2
python-2.7 ×2
python-3.6 ×2
django ×1
function ×1
mypy ×1
python-3.5 ×1
return-type ×1
typing ×1