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