python中的dir()函數(shù),可以在我們不了解某一函數(shù)或方法時(shí),幫我們查看這些方法或函數(shù)的用法及屬性。
dir()函數(shù)說(shuō)明
不帶參數(shù)時(shí),返回當(dāng)前范圍內(nèi)的變量、方法和定義的類型列表;帶參數(shù)時(shí),返回參數(shù)的屬性、方法列表。如果參數(shù)包含方法__dir__(),
該方法將被調(diào)用。如果參數(shù)不包含__dir__(),該方法將限度地收集參數(shù)信息。
參數(shù)object:對(duì)象、變量、類型。
版本:該函數(shù)在python各個(gè)版本中都有,但是每個(gè)版本中顯示的屬性細(xì)節(jié)有所不同。使用時(shí)注意區(qū)別。
例如
>>>importstruct
>>>dir()#showthenamesinthemodulenamespace
['__builtins__','__doc__','__name__','struct']
>>>dir(struct)#showthenamesinthestructmodule
['Struct','__builtins__','__doc__','__file__','__name__',
'__package__','_clearcache','calcsize','error','pack','pack_into',
'unpack','unpack_from']
>>>classShape(object):
def__dir__(self):
return['area','perimeter','location']
>>>s=Shape()
>>>dir(s)
代碼實(shí)例
>>>dir()
['__builtins__','__doc__','__name__','__package__']
>>>importstruct
>>>dir()
['__builtins__','__doc__','__name__','__package__','struct']
>>>dir(struct)
['Struct','__builtins__','__doc__','__file__','__name__','__package__','_clearcache','calcsize','error','pack',
'pack_into','unpack','unpack_from']
>>>classPerson(object):
...def__dir__(self):
...return["name","age","country"]
...
>>>dir(Person)
['__class__','__delattr__','__dict__','__dir__','__doc__','__format__','__getattribute__','__hash__','__init__',
'__module__','__new__','__reduce__','__reduce_ex__','__repr__','__setattr__','__sizeof__','__str__','__subclasshook__',
'__weakref__']
>>>tom=Person()
>>>dir(tom)
['age','country','name']
以上內(nèi)容為大家介紹了python培訓(xùn)之怎么查看幫助函數(shù),希望對(duì)大家有所幫助,如果想要了解更多Python相關(guān)知識(shí),請(qǐng)關(guān)注IT培訓(xùn)機(jī)構(gòu):千鋒教育。http://www.dietsnews.net/