In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to use__init__() in python". The explanation content in this article is simple and clear, easy to learn and understand. Please follow the idea of Xiaobian slowly and deeply to study and learn "how to use__init__() in python" together!
directory
Functions defined in python classes are called methods, and the init () method is essentially a function. This function is used to initialize the object after the instance.
Examples are as follows:
The init() method initializes the object cqueue after the instance.
class CQueue:
Functions in a class are called methods, where the__init__() method is called automatically when the class instantiates. If the class defines the__init__() method, instantiation of the class automatically calls the__init__method.
The__init__method can have parameters that are passed to the instantiation of the class via__init__(). Self is an instance of a class, not a class.
The only difference between a class method and a normal function is that a class method must have an extra first parameter name, which by convention is self.
class CQueue def __init__(self): self.A,self.B=[],[] def appendTail(self,value:int)->None: self.A.append(value) def deleteHead(self)->int: if self.B: return self.B.pop() if not self.A: return -1 while self.A: self.B.append(self.A.pop()) print(self.B) return self.B.pop()if __name__=="__main__": #instantiation of class CQueue, where brackets () cannot be lost. The #__init__() method is used to initialize the object cqueue after the instance. cqueue=CQueue() #Call appendTail () function in class to add elements. cqueue.appendTail(243) cqueue.appendTail(2) cqueue.appendTail(4) cqueue.deleteHead() cqueue.deleteHead()
There is no__init__difference, the conclusion is as follows:
1. When the class instantiates, if there is an__init__method in the class, call the__init__method.
2. If the class has no parent class and no__init__method, nothing happens. That is, the object is not initialized.
3. If the class has a parent class, call the__init__method in the parent class.
The specific experiments are as follows:
Define three classes: CQueue, student, sum, where CQueue and student are the parent classes of sum.
Lab 1: The__init__method initializes the class of the instance.
Lab 2: When a subclass does not have an__init__method, it initializes in the parent class.
Experiments 3 and 4 illustrate: sum(student,CQueue ), when the subclass sum does not have the__init__method, it will call the parent class, and call the parent class ranked first in the parent class, where the student class precedes CQueue, so call the__init__method in the student class to initialize the class of the instance.
Lab 5 illustrates that an instantiated class is not initialized when neither the class nor its parent class is initialized.
Lab 1 Code:
class CQueue: #Functions in a class are called methods, where the__init__() method is called automatically when the class instantiates. If the class defines the__init__() method, instantiation of the class automatically calls the__init__method. The #__init__method can have parameters, which are passed to the instantiation operation of the class via__init__(). Self is an instance of a class, not a class. The only difference between a class method and a normal function is that a class method must have an extra first parameter name, which by convention is self. def __init__(self): self.A,self.B=[],[] def appendTail(self,value:int)->None: self.A.append(value) def deleteHead(self)->int: if self.B: return self.B.pop() if not self.A: return -1 while self.A: self.B.append(self.A.pop()) print(self.B) return self.B.pop()class student: def __init__(self): print("Test Output 1") def test1(self): print("Test Output 2")class sum(student,CQueue ): def __init__(self): print("Test Output 3") def test2(self): print("Test Output 4")if __name__=="__main__": #instantiation of class CQueue, where brackets () cannot be lost. s=sum()
Experimental results:
Lab 2 Code:
class CQueue: #Functions in a class are called methods, where the__init__() method is called automatically when the class instantiates. If the class defines the__init__() method, instantiation of the class automatically calls the__init__method. The #__init__method can have parameters, which are passed to the instantiation operation of the class via__init__(). Self is an instance of a class, not a class. The only difference between a class method and a normal function is that the class method must have an extra first parameter name, which by convention is self. def __init__(self): self.A,self.B=[],[] def appendTail(self,value:int)->None: self.A.append(value) def deleteHead(self)->int: if self.B: return self.B.pop() if not self.A: return -1 while self.A: self.B.append(self.A.pop()) print(self.B) return self.B.pop()class student: def __init__(self): print("Test Output 1") def test1(self): print("Test Output 2")class sum(student,CQueue ): # def __init__(self): # print("Test Output 3") def test2(self): print("Test Output 4")if __name__=="__main__": #instantiation of class CQueue, where brackets () cannot be lost. s=sum()
Experimental results:
Exp. 3, 4, omitted.
Experiment 5:
class CQueue: #Functions in a class are called methods, where the__init__() method is called automatically when the class instantiates. If the class defines the__init__() method, instantiation of the class automatically calls the__init__method. The #__init__method can have parameters, which are passed to the instantiation operation of the class via__init__(). Self is an instance of a class, not a class. The only difference between a class method and a normal function is that the class method must have an extra first parameter name, which by convention is self. def __init__(self): self.A,self.B=[],[] def appendTail(self,value:int)->None: self.A.append(value) def deleteHead(self)->int: if self.B: return self.B.pop() if not self.A: return -1 while self.A: self.B.append(self.A.pop()) print(self.B) return self.B.pop()class student: # def __init__(self): # print("Test Output 1") def test1(self): print("Test Output 2")class sum(student,CQueue ): # def __init__(self): # print("Test Output 3") def test2(self): print("Test Output 4")if __name__=="__main__": #instantiation of class CQueue, where brackets () cannot be lost. s=sum()
Experimental results:
From the results of the experiment, it can be seen that there is no operation.
Thank you for reading, the above is the content of "how to use__init__() in python", after learning this article, I believe that everyone has a deeper understanding of how to use__init__() in python, and the specific use needs to be verified by practice. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.