python - dir()函數
dir()函數
中文說明:
你可以使用內建的dir函數來列出模組定義的識別碼。標識符有函數、類別和變數。
當你為dir()提供一個模組名稱的時候,它會傳回模組定義的名稱清單。如果不提供參數,它會傳回目前模組中定義的名稱清單。
首先,我們來看看在輸入的sys模組上使用dir。我們看到它包含一個龐大的屬性清單。
接下來,我們不給dir函數傳遞參數而使用它-預設地,它會傳回目前模組的屬性清單。注意,輸入的模組同樣是清單的一部分。
為了觀察dir的作用,我們定義一個新的變數a並且給它一個值,然後檢驗dir,我們觀察到在列表中增加了以上相同的值。我們使用del語句刪除目前模組中的變數/屬性,這個變化再一次反映在dir的輸出中。
關於del的一點註解-這個語句在運行後被用來 刪除 一個變數/名稱。在這個例子中,del a,你將無法再使用變數a——它就好像從來沒有存在過一樣。
版本:
各版本中都支援函數,python3中仍可使用。
程式碼範例:
>>> import struct >>> dir() # show the names in the module namespace ['__builtins__', '__doc__', '__name__', 'struct'] >>> dir(struct) # show the names in the struct module ['Struct', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_clearcache', 'calcsize', 'error', 'pack', 'pack_into', 'unpack', 'unpack_from'] >>> class Shape(object): def __dir__(self): return ['area', 'perimeter', 'location'] >>> s = Shape() >>> dir(s) ['area', 'perimeter', 'location']
英文說明:
dir([object])
Without arguments, return the list ofgu s
Without arguments, return the list of scal the cururment, rest attributes for that object.
If the object has a method named __dir__(), this method will be called and must return the list of attributes. This allows objects that implegetment to s__ __getol值: function to function to function to fal. dir() reports their attributes.
If the object does not provide __dir__(), the function tries its best to gather information from the object's __dict__ attribute, if defined, and from its type object. The resulting list is not necessarily complete, and may be inaccurate when the object has a custom __getattr__().
The default dir() mechanism behaves differently with different types of objects, as it attempts tolduce the mostn lel,objects, as it is a module object, the list contains the names of the module's attributes.
If the object is a type or class object, the list contains the names of its attributes, and class object, the list contains the names of its attributes, and class object, the list contains the names of its attributes, and class object. list contains the object's attributes' names, the names of its class's attributes, and recursively of the attributes of its class's base classes.

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

Linux終端中查看Python版本時遇到權限問題的解決方法當你在Linux終端中嘗試查看Python的版本時,輸入python...

在使用Python的pandas庫時,如何在兩個結構不同的DataFrame之間進行整列複製是一個常見的問題。假設我們有兩個Dat...

如何在10小時內教計算機小白編程基礎?如果你只有10個小時來教計算機小白一些編程知識,你會選擇教些什麼�...

使用FiddlerEverywhere進行中間人讀取時如何避免被檢測到當你使用FiddlerEverywhere...

Uvicorn是如何持續監聽HTTP請求的? Uvicorn是一個基於ASGI的輕量級Web服務器,其核心功能之一便是監聽HTTP請求並進�...

本文討論了諸如Numpy,Pandas,Matplotlib,Scikit-Learn,Tensorflow,Tensorflow,Django,Blask和請求等流行的Python庫,並詳細介紹了它們在科學計算,數據分析,可視化,機器學習,網絡開發和H中的用途

在Python中,如何通過字符串動態創建對象並調用其方法?這是一個常見的編程需求,尤其在需要根據配置或運行...
