Python 內建類屬性


本文將向您解釋 Python 中的內建類屬性。

內建類屬性為我們提供了關於類的資訊。

使用點(.)運算子,我們可以訪問內建類屬性。

Python 中的內建類屬性列在下面:

屬性 描述
__dict__ 包含類名稱空間的字典
__doc__ 如果存在類文件字串,則返回它;否則返回 None。
__name__ 類名。
__module__ 定義類的模組名稱。在互動模式下,此屬性為“__main__”。
__bases__ 一個可能為空的元組,包含基類,按照它們在基類列表中出現的順序排列。

現在我們將簡要介紹每個屬性,並透過示例進行說明,以便更好地理解。

示例 1:__dict__ 類屬性

在這裡,我們列印類的字典。

# creating a class say Tutorialspoint class Tutorialspoint: 'Welcome to Tutorialspoint Python' def __init__(self): print("The __init__ method") # Printing the value of Built-in __dict__ attribute for the TutorialsPoint class print(Tutorialspoint.__dict__)

輸出

執行上述程式將生成以下輸出:

{'__module__': '__main__', '__doc__': 'Welcome to Tutorialspoint Python', '__init__': <function Tutorialspoint.__init__ at 0x7fd7812df050>, '__dict__': <attribute '__dict__' of 'Tutorialspoint' objects>, '__weakref__': <attribute '__weakref__' of 'Tutorialspoint' objects>}

示例 2:__doc__ 類屬性

在下面的 Python 程式碼中,我們建立一個帶有文件字串的 Tutorialspoint 類。

# creating a class say Tutorialspoint class Tutorialspoint: 'Welcome to Tutorialspoint Python' def __init__(self): print("The __init__ method") # getting the documentation for the Tutorialspoint using # built-in __doc__ class attribute print(Tutorialspoint.__doc__)

輸出

Welcome to Tutorialspoint Python

示例 3:__name__ 類屬性

在下面的 Python 程式碼中,我們使用 __name__ 類屬性列印類的名稱。

class Tutorialspoint: 'Welcome to Tutorialspoint Python' def __init__(self): print("The __init__ method") # getting the name of the class using built-in __name__ class attribute print(Tutorialspoint.__name__)

輸出

Tutorialspoint

示例 4:__module__ 類屬性

在下面的 Python 程式碼中,我們使用 __module__ 類屬性列印類的模組。

class Tutorialspoint: 'Welcome to Tutorialspoint Python' def __init__(self): print("The __init__ method") # getting the module of the class using built-in __module__ class attribute print(Tutorialspoint.__module__ )

輸出

__main__

示例 5:__bases__ 類屬性

在下面的 Python 程式碼中,我們使用內建的 __bases__ 類屬性列印類的基類。

class Tutorialspoint: 'Welcome to Tutorialspoint Python' def __init__(self): print("The __init__ method") # getting the bases of the class using built-in __bases__ class attribute print(Tutorialspoint.__bases__)

輸出

(<class 'object'>,)

在一個程式中應用所有類屬性

演算法(步驟)

以下是執行所需任務的演算法/步驟:

  • 建立一個名為Employee的父類。

  • 建立一個變數,並將其初始化為 0,用於儲存員工計數。

  • 建立一個init建構函式,該函式接受姓名和工資作為引數。

  • In Python, __init__ is one of the reserved methods. It is referred to as a constructor in object-oriented programming. When an object is created from the class and access is necessary to initialize the class's attributes, the __init__ function is called.
    
  • 初始化例項屬性的值。

  • 將員工計數加 1。

  • 建立一個名為displayCount的函式,該函式列印員工總數。

  • 透過對 Employee 類應用(.)運算子來列印員工計數。

  • 建立一個名為displayEmployee的函式,並列印姓名和工資。

  • 建立一個名為subEmployee的子類,該類繼承自父類 Employee 類。

  • 使用__doc__類屬性列印帶有文件字串的 Employee 類。

  • 使用__name__類屬性列印 Employee 類的名稱。

  • 使用__module__類屬性列印 Employee 類的模組。

  • 使用__dict__類屬性列印包含 Employee 類名稱空間的字典。

  • 使用__bases__類屬性列印派生類所有基類。

示例

# Creating a parent class with the name Employee class Employee: 'Common base class for all employees' # initiaizing the variable with 0 for storing the employ count empCount = 0 # init constructor which accepts the name, salary as arguments def __init__(self, name, salary): # Initialize the values of instance attributes self.name = name self.salary = salary # incrementing the employ count by 1 Employee.empCount += 1 # displayCount () which prints the total employ count def displayCount(self): print("Total Employee :",Employee.empCount) # creating another function i.e, displayEmployee def displayEmployee(self): print ("Name : ", self.name, ", Salary: ", self.salary) # creating child class with the name subEmployee inheriting properties # from the parent Employee class class subEmployee(Employee): 'Derived class for Base - Employee Class' # printing Employee class with documentation using __doc__ class attribute print("Employee class documentation using the __doc__ attribute:\n", Employee.__doc__) # printing the name of the Employee class using __name__ class attribute print("Name of the class using the __name__ attribute:\n", Employee.__name__) # printing the module of the Employee class using __module__ class attribute print("Module of the class using the __module__ attribute:\n", Employee.__module__) # printing the dictionary containing the Employee class namespace # using __dict__ class attribute print("Dictionary containing the Employee class namespace using the __dict__ attribute:\n", Employee.__dict__) # printing all the base classes for the derived class using __bases__ class attribute print("Base classes of subEmployee class using the __bases__ attribute:\n", subEmployee.__bases__)

輸出

Employee class documentation using the __doc__ attribute:
Common base class for all employees
Name of the class using the __name__ attribute:
Employee
Module of the class using the __module__ attribute:
__main__
Dictionary containing the Employee class namespace using the __dict__ attribute:
{'__module__': '__main__', '__doc__': 'Common base class for all employees', 'empCount': 0, '__init__': <function Employee.__init__ at 0x7f9532090050>, 'displayCount': <function Employee.displayCount at 0x7f95320900e0>, 'displayEmployee': <function Employee.displayEmployee at 0x7f9532090170>, '__dict__': <attribute '__dict__' of 'Employee' objects>, '__weakref__': <attribute '__weakref__' of 'Employee' objects>}
Base classes of subEmployee class using the __bases__ attribute:
(<class '__main__.Employee'>,)

結論

從本文中,我們學習了所有五個內建類屬性及其示例。我們還透過使用兩個類演示瞭如何在實踐中使用這五個內建類屬性。

更新於:2022年9月22日

17K+ 次瀏覽

啟動您的職業生涯

透過完成課程獲得認證

開始
廣告
© . All rights reserved.