1.-有没有可能在链式赋值中添加类型提示?
这两次尝试都失败了:
>>> def foo(a:int):
... b: int = c:int = a
File "<stdin>", line 2
b: int = c:int = a
^
SyntaxError: invalid syntax
>>> def foo(a:int):
... b = c:int = a
File "<stdin>", line 2
b = c:int = a
^
SyntaxError: invalid syntax
这些是我的尝试:
>>> from typing import Tuple
>>> def bar(a: Tuple[int]):
... b: int, c:int = a
File "<stdin>", line 2
b: int, c:int = a
^
SyntaxError: invalid syntax
>>> def bar(a: Tuple[int]):
... b, c:Tuple[int] = a
...
File "<stdin>", line 2
SyntaxError: only single target (not tuple) can be annotated
>>> def bar(a: Tuple[int]):
... b, c:int = a
...
File "<stdin>", line 2
SyntaxError: only single target (not tuple) can be annotated
我知道在这两种情况下,类型都是从a的类型提示推断出来的,但是我有一个很长的变量列表(在
__init__
我想说得更清楚。
我使用的是python3.6.8。