python中dir()函數(shù)不帶參數(shù)時(shí),返回當(dāng)前范圍內(nèi)的變量、方法和定義的類型列表;帶參數(shù)時(shí),返回參數(shù)的屬性、方法列表。如果參數(shù)包含方法__dir__(),該方法將被調(diào)用。如果參數(shù)不包含__dir__(),該方法將限度地收集參數(shù)信息。
dir語法:
dir([object])
參數(shù)說明
object--對(duì)象、變量、類型。
返回值
返回模塊的屬性列表。
以下實(shí)例展示了dir的使用方法:
>>>dir()#獲得當(dāng)前模塊的屬性列表
['__builtins__','__doc__','__name__','__package__','arr','myslice']
>>>dir([])#查看列表的方法
['__add__','__class__','__contains__','__delattr__','__delitem__','__delslice__','__doc__','__eq__',
'__format__','__ge__','__getattribute__','__getitem__','__getslice__','__gt__','__hash__','__iadd__',
'__imul__','__init__','__iter__','__le__','__len__','__lt__','__mul__','__ne__','__new__','__reduce__',
'__reduce_ex__','__repr__','__reversed__','__rmul__','__setattr__','__setitem__','__setslice__','__sizeof__',
'__str__','__subclasshook__','append','count','extend','index','insert','pop','remove','reverse','sort']
>>>
以上內(nèi)容為大家介紹了python培訓(xùn)之dir是什么,希望對(duì)大家有所幫助,如果想要了解更多Python相關(guān)知識(shí),請(qǐng)關(guān)注IT培訓(xùn)機(jī)構(gòu):千鋒教育。