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.

  • 相关阅读:
    模版
    列表项模版
    vue eventBus 跳坑的办法
    vue适配移动端px自动转化为rem
    pc端,移动端css重置样式
    vue全局引入scss文件(推荐)
    vue安装scss,并且全局引入
    mapState
    通俗易懂的vuex-demo
    ve2.0 v-for循环报错的解决方案
  • 原文地址:https://www.cnblogs.com/morganh/p/15048664.html
Copyright © 2011-2022 走看看