您能为使用with
语法定义的变量定义类型提示吗?
with example() as x:
print(x)
我想输入上面的提示来说明x
是str
(作为一个例子).
我发现唯一的解决方法是使用中间变量,但这感觉有点粗糙.
with example() as x:
y: str = x
print(y)
我找不到typing documentation个例子.
您能为使用with
语法定义的变量定义类型提示吗?
with example() as x:
print(x)
我想输入上面的提示来说明x
是str
(作为一个例子).
我发现唯一的解决方法是使用中间变量,但这感觉有点粗糙.
with example() as x:
y: str = x
print(y)
我找不到typing documentation个例子.
PEP 526已经在Python 3.6中实现,它允许您对变量进行注释.例如,你可以使用,
x: str
with example() as x:
[...]
或
with example() as x:
x: str
[...]