天天看點

python3中 ->的意義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:

繼續閱讀