【Python面試真題】- 解釋Python中的help()和dir()函數(shù)
Help()函數(shù)是一個內(nèi)置函數(shù),用于查看函數(shù)或模塊用途的詳細(xì)說明:
>>> import copy
>>> help(copy.copy)
運(yùn)行結(jié)果為:
Help on function copy in module copy:
copy(x)
Shallow copy operation on arbitrary Python objects.
See the module’s __doc__ string for more info.
Dir()函數(shù)也是Python內(nèi)置函數(shù),dir() 函數(shù)不帶參數(shù)時,返回當(dāng)前范圍內(nèi)的變量、方法和定義的類型列表;帶參數(shù)時,返回參數(shù)的屬性、方法列表。
以下實(shí)例展示了 dir 的使用方法:
>>> dir(copy.copy)
運(yùn)行結(jié)果為:
[‘__annotations__’, ‘__call__’, ‘__class__’, ‘__closure__’, ‘__code__’, ‘__defaults__’, ‘__delattr__’, ‘__dict__’, ‘__dir__’, ‘__doc__’, ‘__eq__’, ‘__format__’, ‘__ge__’, ‘__get__’, ‘__getattribute__’, ‘__globals__’, ‘__gt__’, ‘__hash__’, ‘__init__’, ‘__init_subclass__’, ‘__kwdefaults__’, ‘__le__’, ‘__lt__’, ‘__module__’, ‘__name__’, ‘__ne__’, ‘__new__’, ‘__qualname__’, ‘__reduce__’, ‘__reduce_ex__’, ‘__repr__’, ‘__setattr__’, ‘__sizeof__’, ‘__str__’, ‘__subclasshook__’]