新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
創(chuàng)新互聯(lián)Python教程:python如何查看類的函數(shù)
python非常方便,它不需要用戶查詢文檔,只需掌握如下兩個(gè)幫助函數(shù),即可查看Python中的所有函數(shù)(方法)以及它們的用法和功能:

dir():列出指定類或模塊包含的全部?jī)?nèi)容(包括函數(shù)、方法、類、變量等)。
help():查看某個(gè)函數(shù)或方法的幫助文檔。
相關(guān)推薦:《Python基礎(chǔ)教程》
例如,要查看字符串變量(它的類型是 str 類型)所能調(diào)用的全部?jī)?nèi)容,可以在交互式解釋器中輸入如下命令:
>>> dir(str) ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill'] >>>
上面列出了字符串類型(str)提供的所有方法,其中以“_”開頭、“_”結(jié)尾的方法被約定成私有方法,不希望被外部直接調(diào)用。
如果希望查看某個(gè)方法的用法,則可使用 help() 函數(shù)。例如,在交互式解釋器中輸入如下命令:
>>> help(str.title) Help on method_descriptor: title(...) S.title() -> str Return a titlecased version of S, i.e. words start with title case characters, all remaining cased characters have lower case. >>>
從上面介紹可以看出,title()方法的使用形式是“str.title()”,其功能是將字符串中所有單詞的首字母大寫,其他所有字符全部改為小寫。
通過使用 dir() 和 help() 函數(shù),我們就可以查看字符串變量所能調(diào)用的所有方法,包括他們的使用方法和功能等。
當(dāng)前題目:創(chuàng)新互聯(lián)Python教程:python如何查看類的函數(shù)
網(wǎng)站網(wǎng)址:http://www.dlmjj.cn/article/cdhgghd.html


咨詢
建站咨詢
