寫程式碼的時候常常會突然對一些模組和函數不清楚,現在就來教你怎麼透過dir() 和 help() 尋找說明或屬性!!趕快繃緊神經學習!

寫程式碼的時候常常會突然對一些模組和函數不清楚,現在就來教你怎麼透過dir() 和 help() 尋找說明或屬性!!趕快繃緊神經學習!

Python 中 dir()、help()的使用 在使用Python來編寫程式碼的時候,常常會使用 Python 中的內建函數 (Built-in Functions)或是模組。如果當你對一些函數、模組不清楚的話,就能透過 dir() 和 help() 尋找說明或屬性。 help(): 用於查看屬性及方法的詳細解釋

Python 內建函數 - dir()

「dir()」的用途是用於用來查詢物件的全部屬性。比如說今天你想使用 dir() 函數用於查看物件「str」 的全部屬性,你就寫了:
>>>print 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', 'isascii', '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']
如果 dir() 的括弧內不帶任何參數物件,執行結果則會最大限度地顯示出當前範圍內的變數、方法和屬性列表。
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__']

Python 內建函數 - help()

help(): 用於查看函式或模組用途的詳細說明。例如說今天你想使用 help() 用於查看「dir」 的用途,你就寫了:
>>>help(‘dir’)
執行結果如下:
Help on built-in function dir in module builtins:

dir(…)
    dir([object]) -> list of strings

    If called without an argument, return the names in the current scope.
    Else, return an alphabetized list of names comprising (some of) the attributes
    of the given object, and of attributes reachable from it.
    If the object supplies a method named __dir__, it will be used; otherwise
    the default dir() logic is used and returns:
      for a module object: the module’s attributes.
      for a class object:  its attributes, and recursively the attributes
        of its bases.
      for any other object: its attributes, its class’s attributes, and
        recursively the attributes of its class’s base classes.

 

 

推薦閱讀:

如果不會Python連白領階級的人都有可能被取代?

Python是程式語言之冠?把其他程式語言當什麼了?(上)

Python是程式語言之冠?把其他程式語言當什麼了?(下)

神奇的Java課程-竟可讓人從零經驗到成為工程師不用一年!!?

暑假快到了!與其讓兒子盲目拚學測,我寧可讓他先選擇人生方向~

單一技能已被淘汰 讓Java課程使你充實自我 還能媒合高薪職缺

程式課程選達內,美上市IT課程教育集團

 

arrow
arrow

    uidesignxiaoshow 發表在 痞客邦 留言(0) 人氣()