Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在尝试输入提示海象运算符表达式,即
while (var: int := some_func()): ...
我怎样才能做到这一点?
这是不可能的。来自PEP 572
不支持内联类型注释:
您需要在循环之前声明变量while,并且可以在那里指定类型。
while
var: int while var := some_func(): ...
我不相信你可以。
变量可以被注释,因为赋值的语法规则是
assignment: | NAME ':' expression ['=' annotated_rhs ] ...
请注意,类型提示:在名称和=.
:
=
另一方面,赋值表达式只提供名称,没有类型提示,位于:=:
:=
named_expression: | NAME ':=' ~ expression | expression !':='