python3 in -> significance

Reference website:
https://stackoverflow.com/questions/14379753/what-does-mean-in-python-function-definitions

python3 in -> significance

  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 is also supported parameter annotations parameters Note
    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:

Published 98 original articles · won praise 124 · views 30000 +

Guess you like

Origin blog.csdn.net/lyc0424/article/details/104956215