python中的dir()函數,可以在我們不了解某一函數或方法時,幫我們查看這些方法或函數的用法及屬性。
dir()函數說明
不帶參數時,返回當前范圍內的變量、方法和定義的類型列表;帶參數時,返回參數的屬性、方法列表。如果參數包含方法__dir__(),
該方法將被調用。如果參數不包含__dir__(),該方法將限度地收集參數信息。
參數object:對象、變量、類型。
版本:該函數在python各個版本中都有,但是每個版本中顯示的屬性細節有所不同。使用時注意區別。
例如
>>>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)
代碼實例
>>>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']
以上內容為大家介紹了python培訓之怎么查看幫助函數,希望對大家有所幫助,如果想要了解更多Python相關知識,請關注IT培訓機構:千鋒教育。http://www.mycheba.com/