本文目錄一覽:
python中的「dir」和「help」作用是什麼?
dir和help是Python中兩個強大的built-in函數,就像Linux的man一樣,絕對是開發的好幫手。比如查看list的所以屬性:
dir(list)
輸出:
[‘__add__’, ‘__class__’, ‘__contains__’, ‘__delattr__’, ‘__delitem__’, ‘__delslice__’, ‘__doc__’, ‘__eq__’, ‘__format__’, ‘__ge__’, ‘__getattribute__’, ‘__getitem__’, ‘__getslice__’, ‘__gt__’, ‘__hash__’, ‘__iadd__’, ‘__imul__’, ‘__init__’, ‘__iter__’, ‘__le__’, ‘__len__’, ‘__lt__’, ‘__mul__’, ‘__ne__’, ‘__new__’, ‘__reduce__’, ‘__reduce_ex__’, ‘__repr__’, ‘__reversed__’, ‘__rmul__’, ‘__setattr__’, ‘__setitem__’, ‘__setslice__’, ‘__sizeof__’, ‘__str__’, ‘__subclasshook__’, ‘append’, ‘count’, ‘extend’, ‘index’, ‘insert’, ‘pop’, ‘remove’, ‘reverse’, ‘sort’]
然後查看list的pop方法的作用和用法:
help(list.pop)
輸出:
Help on method_descriptor:
pop(…)
L.pop([index]) – item — remove and return item at index (default last).
Raises IndexError if list is empty or index is out of range.
(END)
python的dir和help用法
當你給dir()提供一個模塊名字時,它返回在那個模塊中定義的名字的列表。當沒有為其提供參數時,
它返回當前模塊中定義的名字的列表。
dir()
函數使用舉例:
import sys # 獲得屬性列表,在這裡是sys模塊的屬性列表
dir(sys)
[‘__displayhook__’, ‘__doc__’, ‘__excepthook__’, ‘__name__’,
‘__package__’, ‘__stderr__’, ‘__stdin__’, ‘__stdout__’,
‘_clear_type_cache’, ‘_compact_freelists’,’_current_frames’,
‘_getframe’, ‘api_version’, ‘argv’, …]
如果您需要快速獲取任何的Python函數或語句的信息,那麼您可以使用內置的「help」(幫助)功能。這是非常有用的,尤其是當使用翻譯提示符時,例如,運行『help(print)」——這將顯示print函數的幫助--用於列印東西到屏幕上。
help()函數使用舉例:
help(print)
Help on built-in function print in module builtins:
print(…)
print(value, …, sep=’ ‘, end=’\n’, file=sys.stdout, flush=False)
…
python語言中的內建函數dir()是幹啥用的啊?
dir() 函數
儘管查找和導入模塊相對容易,但要記住每個模塊包含什麼卻不是這麼簡單。您並不希望總是必須查看源代碼來找出答案。幸運的是,Python 提供了一種方法,可以使用內置的 dir() 函數來檢查模塊(以及其它對象)的內容。
dir() 函數可能是 Python 自省機制中最著名的部分了。它返回傳遞給它的任何對象的屬性名稱經過排序的列表。如果不指定對象,則 dir() 返回當前作用域中的名稱
python dir和vars的區別
dir():默認列印當前模塊的所有屬性,如果傳一個對象參數則列印當前對象的屬性
vars():默認列印當前模塊的所有屬性,如果傳一個對象參數則列印當前對象的屬性
vars():函數以字典形式返回參數中每個成員的當前值,如果vars函數沒有帶參數,那麼它會返回包含當前局部命名空間中所有成員的當前值的一個字典。
help(vars)
Help on built-in function vars in module __builtin__:
vars(…)
vars([object]) – dictionary
Without arguments, equivalent to locals().
With an argument, equivalent to object.__dict__.
dir()和vars()的區別就是:dir()只列印屬性,vars()則列印屬性與屬性的值。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/289475.html