Python dir()函數(shù)

版本

E:\Projects\testTool>python --version
Python 3.6.2

先看看官網(wǎng)上是怎么描述dir()函數(shù)的:
Without arguments, return the list of names in the current local scope. With an argument, attempt to return a list of valid attributes for that object.
翻譯過來就是:
如果不向dir()提供參數(shù),返回當前范圍的名稱列表,帶參數(shù)時,返回參數(shù)的名稱列表。

示例

  1. 直接調(diào)用dir()
>>> dir()
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__']
  1. 在當前模塊引入一個包
>>> dir()
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'sys']

執(zhí)行后的結(jié)果包含了引入的包的名字
3.獲得sys模塊的屬性

>>> dir(sys)
['__displayhook__', '__doc__', '__excepthook__', '__interactivehook__', '__loader__', '__name__', '__package__', '__spec__', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_debugmallocstats', '_enablelegacywindowsfsencoding', '_getframe', '_git', '_home', '_xoptions', 'api_version', 'argv', 'base_exec_prefix', 'base_prefix', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dllhandle', 'dont_write_bytecode', 'exc_info', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'float_repr_style', 'get_asyncgen_hooks', 'get_coroutine_wrapper', 'getallocatedblocks', 'getcheckinterval', 'getdefaultencoding', 'getfilesystemencodeerrors', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'getswitchinterval', 'gettrace', 'getwindowsversion', 'hash_info', 'hexversion', 'implementation', 'int_info', 'intern', 'is_finalizing', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'ps1', 'ps2', 'set_asyncgen_hooks', 'set_coroutine_wrapper', 'setcheckinterval', 'setprofile', 'setrecursionlimit', 'setswitchinterval', 'settrace', 'stderr', 'stdin', 'stdout', 'thread_info', 'version', 'version_info', 'warnoptions', 'winver']

4.在當前模塊添加一個變量

>>> a = 5 #創(chuàng)建了一個新變量
>>> dir()
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'a', 'sys']

5.在當前模塊添加一個函數(shù)

>>> def function():pass #添加一個函數(shù)
>>> dir()
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'a', 'function', 'sys']

6.在當前模塊添加一個類

>>> class Testcls(): #新建一個類
...     def __init__(self):
...         self.a = 1
...         self.b = 2
...     def add_func(self):
...         return self.a + self.b
>>> dir()
['Testcls', '__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__', 'a', 'function', 'sys']

查看類Testcls的屬性:

>>> dir(Testcls)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'add_func']

總結(jié)

通過以上的示例,python提供的dir()函數(shù),其實就是查看當前域的變量、方法和類,在我們不知道一個模塊或庫或類有哪些屬性時,使用dir()很方便。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,506評論 19 139
  • 要點: 函數(shù)式編程:注意不是“函數(shù)編程”,多了一個“式” 模塊:如何使用模塊 面向?qū)ο缶幊蹋好嫦驅(qū)ο蟮母拍?、屬性?..
    victorsungo閱讀 1,694評論 0 6
  • 我想你了 可是不能對你說 就像開滿桃花的樹上 永遠結(jié)不出青杏 我想你了 可是不知道你在哪 就像夏天的雨 永遠看不到...
    寧靜致遠_6736閱讀 255評論 0 0
  • 第一天 在醫(yī)院待了幾天,爸爸生病住院了,醫(yī)生診斷冠心病,住院治療。 住進去的第一天,病房里已經(jīng)住了倆個病人了,都是...
    月兒的2016閱讀 1,550評論 5 4
  • 從雙十一到雙旦節(jié),我是月月剁手,月月吃土。最近整理了一下購買記錄,發(fā)現(xiàn)好多小東西,用起來滿滿的都是幸福感。下面為你...
    汪煒煒閱讀 5,340評論 2 32

友情鏈接更多精彩內(nèi)容