zoukankan      html  css  js  c++  java
  • What does -> mean in Python function definitions?

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

    https://www.python.org/dev/peps/pep-3107/

    Wow, I missed quite a broad area of knowledge - not only return value annotations, but also parameter annotations. Thank you very much :)

    And the __annotations__ attribute is a dictionary. The key return is the one used to retrieve the value after the arrow.

    >>> def kinetic_energy(m:'in KG', v:'in M/S')->'Joules': 
    ...    return 1/2*m*v**2
    ... 
    >>> kinetic_energy.__annotations__
    {'return': 'Joules', 'v': 'in M/S', 'm': 'in KG'}


    >>> rd={'type':float,'units':'Joules','docstring':'Given mass and velocity returns kinetic energy in Joules'}
    >>> def f()->rd:
    ...    pass
    >>> f.__annotations__['return']['type']
    <class 'float'>
    >>> f.__annotations__['return']['units']
    'Joules'
    >>> f.__annotations__['return']['docstring']
    'Given mass and velocity returns kinetic energy in Joules'

    -> is introduced in python3.

    In simpler words, the content after the -> denotes the return type of the function. The return type is optional.

  • 相关阅读:
    Jmeter中的变量(三)
    Jmeter组件和属性(二)
    Jmeter配置元件执行顺序
    Fiddler Web Session 列表(1)
    selenium webdriver操作各浏览器
    java1.8环境配置+win10系统
    python函数库及函数标准库
    MySql 数据库基础命令
    Linux 常用命令
    normalize.css
  • 原文地址:https://www.cnblogs.com/morganh/p/15048664.html
Copyright © 2011-2022 走看看