python 物件型態取得使用方法協助 dir

python  物件型態取得使用方法協助-dir 指令


當你對python物件使用上有不清楚或想知道更多使用方法時,可利用dir或help的指令直接對已指定值的變數或方法套上這兩個指令即可。


當你呼叫內建的dir函式時,會傳回來指定物件可用的屬性及方法串列。



>>> S='hello'
>>> dir(S)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__
ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__g
t__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__
', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '
__rmul__', '__setattr__', '__str__', 'capitalize', 'center', 'count', 'decode',
'encode', 'endswith', 'expandtabs', 'find', 'index', 'isalnum', 'isalpha', 'isdi
git', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lst
rip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit'
, 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', '
translate', 'upper', 'zfill']
>>>


dir 會顯示此物件的所有方法串列,有底線的代表是此物件的實作,前後雙底線的是python用於實作細節的命名模式,一般我們是用不到。串列中沒有底線的名稱是可以對此物件呼叫的方法或屬性。