Python def longestPalindrome(self, s: str) -> 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)

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

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM