-> Python · 符号
函数返回类型注解,说明函数“应该返回什么类型”,只给人和类型检查工具看。
怎么用
def 函数() -> 类型:
小例子
def 加一(n: int) -> int:
return n + 1
print(加一(3))
运行结果
4
提醒:写错类型不会报错,运行时不检查。
官方文档:https://docs.python.org/zh-cn/3/reference/compound_stmts.html
在代码里遇到不认识的词?打开代码点读机,点一下就懂 → ← Python全部词条