构造函数是一种特殊类型的方法(函数),用于初始化类的实例成员。
构造函数可以有两种类型。
参数化构造函数
非参数化构造函数
构造器定义在我们创建此类的对象时执行。构造函数还验证对象是否有足够的资源来执行任何启动任务。
在python中创建构造函数
在python中,方法__ init __模拟类的构造函数。在实例化类时调用此方法。我们可以在创建类对象时传递任意数量的参数,具体取决于__ init __ definition。它主要用于初始化类属性。每个类都必须有一个构造函数,即使它只依赖于默认构造函数。
请考虑以下示例来初始化Employee类属性。
例
class Employee:  
    def __init__(self,name,id):  
        self.id = id;  
        self.name = name;  
    def display (self):  
        print("ID: %d \nName: %s"%(self.id,self.name))  
emp1 = Employee("John",101)  
emp2 = Employee("David",102)  
  
#accessing display() method to print employee 1 information  
   
emp1.display();   
  
#accessing display() method to print employee 2 information  
emp2.display();输出:
ID: 101 Name: John ID: 102 Name: David
示例:计算类的对象数
class Student:  
    count = 0  
    def __init__(self):  
        Student.count = Student.count + 1  
s1=Student()  
s2=Student()  
s3=Student()  
print("The number of students:",Student.count)Output:
The number of students: 3
Python非参数化构造函数示例
class Student:    
    # Constructor - non parameterized    
    def __init__(self):    
        print("This is non parametrized constructor")    
    def show(self,name):    
        print("Hello",name)    
student = Student()    
student.show("John")Output:
This is non parametrized constructor Hello John
Python参数化构造函数示例
class Student:    
    # Constructor - parameterized    
    def __init__(self, name):    
        print("This is parametrized constructor")    
        self.name = name    
    def show(self):    
        print("Hello",self.name)    
student = Student("John")    
student.show()Output:
This is parametrized constructor Hello John
Python内置类函数
下表中描述了类中定义的内置函数。
| SN | 功能 | 描述 | 
| 1 | getattr(obj,name,default) | 它用于访问对象的属性。 | 
| 2 | setattr(obj, name,value) | 它用于将特定值设置为对象的特定属性。 | 
| 3 | delattr(obj, name) | 它用于删除特定属性。 | 
| 4 | hasattr(obj, name) | 如果对象包含某些特定属性,则返回true。 | 
例
class Student:  
    def __init__(self,name,id,age):  
        self.name = name;  
        self.id = id;  
        self.age = age  
  
#creates the object of the class Student  
s = Student("John",101,22)  
  
#prints the attribute name of the object s  
print(getattr(s,'name'))  
  
# reset the value of attribute age to 23  
setattr(s,"age",23)  
  
# prints the modified value of age  
print(getattr(s,'age'))  
  
# prints true if the student contains the attribute with name id  
  
print(hasattr(s,'id'))  
# deletes the attribute age  
delattr(s,'age')  
  
# this will give an error since the attribute age has been deleted  
print(s.age)Output:
John 23 True AttributeError: 'Student' object has no attribute 'age'
内置类属性
与其他属性一起,python类还包含一些内置的类属性,这些属性提供有关类的信息。
内置类属性在下表中给出。
| SN | 属性 | 描述 | 
| 1 | __dict__ | 它提供包含类命名空间信息的字典。 | 
| 2 | __doc__ | 它包含一个包含类文档的字符串 | 
| 3 | __name__ | 它用于访问类名。 | 
| 4 | _module__ | 它用于访问定义此类的模块。 | 
| 5 | __bases__ | 它包含一个包含所有基类的元组。 | 
例
class Student:  
    def __init__(self,name,id,age):  
        self.name = name;  
        self.id = id;  
        self.age = age  
    def display_details(self):  
        print("Name:%s, ID:%d, age:%d"%(self.name,self.id))  
s = Student("John",101,22)  
print(s.__doc__)  
print(s.__dict__)  
print(s.__module__)Output:
None
{'name': 'John', 'id': 101, 'age': 22}
__main__ 
             第1章 Python简介
第1章 Python简介