Python 函数头的问题有人了解吗? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style rom The Hitchhiker's Guide
s04

Python 函数头的问题有人了解吗?

  •  
  •   s04 May 20, 2018 4512views
    This topic created in 2899 days ago, the information mentioned may be changed or developed.
    def say_hi(name: str, age: int) -> str:
    """
    Hi!
    """
    # your code here
    return "Hi. My name is %s and I'm %d years old" % (name, age)

    if __name__ == '__main__':
    #These "asserts" using only for self-checking and not necessary for auto-testing
    assert say_hi("Alex", 32) == "Hi. My name is Alex and I'm 32 years old", "First"
    assert say_hi("Frank", 68) == "Hi. My name is Frank and I'm 68 years old", "Second"
    print('Done. Time to Check.')
    ------------------------------------------------------------------------------
    我不太明白 say_hi(name: str, age: int) -> str:这个函数头是什么含义。name: str, age: int 是不是 C 里边的类型声明?-> str:这块完全不理解。

    google 能力有限,求各位大佬解答,谢谢。
    10 replies    2018-05-20 14:38:29 +08:00
    Arnie97
        1
    Arnie97  
       May 20, 2018 via Android   1
    threee333
        2
    threee333  
       May 20, 2018 via Android   1
    这玩意叫 annotation (注解),Python3 新加的特性。IDE 会根据注解信息来提供相应的方法。
    代码里一般用不到~如果想玩玩的话,去看看 inspect 模块吧~
    lxian2
        3
    lxian2  
       May 20, 2018   1
    type hint, 标记参数 /返回的类型,帮助 IDE 或者其他工具更准确的帮你做类型检查 /重构。(像 python 这样的鸭子类型的语言工具没办法准确 refactoring)
    mseasons
        4
    mseasons  
       May 20, 2018
    google "Python 函数 冒号"
    afpro
        5
    afpro  
       May 20, 2018
    type hints 就是暗示 IDE 你方法参数和返回值的类型 如果使用的 python 版本比较低 也可以在注释里面通过
    :type 和 :rtype 暗示 IDE
    msg7086
        6
    msg7086  
       May 20, 2018
    > name: str

    你见过哪个 C 语言代码是这么写类型的?你说 Pascal 里的也就算了……
    RedFlag2233
        7
    RedFlag2233  
       May 20, 2018 via Android
    python cookbook3 上第 224 页
    Kendall
        8
    Kendall  
       May 20, 2018 via Android
    不是强制的 函数参数类型的注释
    xiaoheijw
        9
    xiaoheijw  
       May 20, 2018   1
    这个是为了方便别人读代码。毕竟动态语言没有强制规定参数类型。
    simple2025
        10
    simple2025  
       May 20, 2018 via Android
    dict 怎么注解想 ts 那样
    About     Help     Advertise     Blog     API     FAQ     Solana     4366 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 71ms UTC 05:31 PVG 13:31 LAX 22:31 JFK 01:31
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86