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.