我想这两个问题是相关的,所以我将把它们放在一起:
1.- Is it possible to put type hint in chained assignments?
这两次try 都失败了:
>>> 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
2.- Is it possible to put type hint in multiple assignments?
以下是我的try :
>>> 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__
中),我希望是额外显式的.
我使用的是Python 3.6.8.