当前位置:首页 > Python教程 > python技巧

Python def longestPalindrome(self, s: str) -> str: 函数功能注释 annotations

str: 函数功能注释 annotations' />

https://www.cnblogs.com/hongten/p/hongten_python_function_annotation.html

 

            
                def f(ham: 42, eggs: int = spam) -> "Nothing to see here":
    
            print("Annotations:", f.__annotations__)
    print("Arguments:", ham, eggs)

#def关键字定义了函数f,在函数f中有两个参数:ham,eggs。
#其中ham没有默认值,而eggs是由默认值的,其默认值为‘spam‘.
#参数ham的注释部分为:42;参数eggs的注释部分为:int
# "Nothing to see here"是返回值的注释,这个必须用 ‘->‘连接#看了java代码后,你会有更直观的认识,注释嘛,你可以根据你自己的想法,想怎么写就怎么写,如42,int;不过不好的注释有时候会给别人阅读代码带来很大的麻烦#如果上面的代码是这样写:def f(ham: int = 42, eggs: str = spam) -> Nothing to see here:
    print("Annotations:", f.__annotations__)
    print("Arguments:", ham, eggs)

#我想很多人阅读代码的时候就很容易理解啦 

 

函数功能注释 annotations' ref='nofollow'>Python def longestPalindrome(self, s: str) -> str: 函数功能注释 annotations

原文:https://www.cnblogs.com/focus-z/p/12312035.html


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!

相关教程推荐

其他课程推荐