Python 内置类属性

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 构造函数,接受名称和工资作为参数。

在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'>,)

结论

我们通过这篇文章学习了所有五个内置类属性的示例。我们还通过使用两个类来展示这五个内置类属性在实践中的使用。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程