Python 中的内置类属性

pythonserver side programmingprogramming更新于 2025/5/8 8:44:17

在本文中,我们将向您解释 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' object>, '__weakref__': <attribute '__weakref__' of 'Tutorialspoint' object>}

示例 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 构造函数,接受名称和薪水作为参数。

  • 在 Python 中,__init__ 是保留方法之一。它在面向对象编程中被称为构造函数。当从类创建对象并且需要访问以初始化类的属性时,将调用 __init__ 函数。
    
  • 初始化实例属性的值

  • 将雇员数量增加 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'>,)

结论

我们通过本文中的示例学习了所有五个内置类属性。我们还通过使用两个类演示了如何在实践中使用这五个内置类属性。


相关文章


有用资源