python3中 ->的意義

參考網址:
https://stackoverflow.com/questions/14379753/what-does-mean-in-python-function-definitions

python3中 ->的意義

  1. According to this, the example you’ve supplied:

     def f(x) -> 123:
         return x
    

will be forbidden in the future (and in current versions will be confusing), it would need to be changed to:

def f(x) -> int:
    return x

for it to effectively describe that function f returns an object of type int.

  1. python中也支持parameter annotations參數註釋
    Python ignores it. In the following code:

     def f(x) -> int:
         return int(x)
    

the -> int just tells that f() returns an integer. It is called a return annotation, and can be accessed as f.annotations[‘return’].

Python also supports parameter annotations:

def f(x: float) -> int:
    return int(x)

: float tells people who read the program (and some third-party libraries/programs, e. g. pylint) that x should be a float. Itis accessed as f.annotations[‘x’], and doesn’t have any meaning by itself. See the documentation for more information:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章