In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what are the basic knowledge of Python". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the basic knowledge of Python"?
1. Basic knowledge of the class
When it comes to classes, we have to mention process-oriented programming and object-oriented programming. Here is an example to illustrate the difference in coding between the two programming methods, as shown in the following figure:
(1) the most important concepts in object-oriented programming are Class and Instance.
(2) A class is an abstract template that can be understood as a basic template, such as the Student class. The instance is a concrete object created from the class. "birds of a feather flock together, people are divided into groups", you can understand the class in programming from this sentence. A class should be a collection of things that have common characteristics, such as human beings and animals. Objects in classes should have common characteristics that correspond to common properties and methods of classes in programming. And there are more or less differences between specific objects, for example, in the "bird" category, each bird has its own characteristics.
(3) each object has the same method, but its own data may be different. Each object has the same method, and the instance owns all the methods in the class; their methods are the same no matter how different the names of the instances are. For example, if the class is Student, whether it is instance lilei or instance hanmeimci, as long as they are all instances of Student, they all have the same methods. The data for each instance may be different, because when using an instance, if you need to pass parameters in this class as the initial data, then each instance may be passed in different data. For example, in a database class, if you pass in a different database connection string, the data of the instance is different.
2. The use of classes
(1) in Python, the definition class is implemented through the class keyword:
Class Student (object):
Pass
Class is followed by the class name, Student. Class names are usually named in uppercase letters, such as StudentCore.
(object) indicates the class from which the class is inherited. If there is no suitable inheritance class, you usually use the object class, which all classes inherit.
(2) instantiating a class is achieved by adding parentheses "()" after the class name:
Pupil=Student ()
"pupil" is an example of "student". The so-called instance can be understood as the embodiment of the class. The class cannot be used directly, it can only be instantiated first, then the class is represented by an instance, and then the methods in the class are called to process the data. So how does an instance represent a class? When defining a class and its methods, there is a parameter called self, which is the key parameter between the concatenated instance and the class, which can be simply understood as "self= instance name".
There are properties and methods under the class. Attributes can be understood as static data and methods as dynamic processing functions. For example, the "student" class is defined in the following code, where _ _ init_ _ is the property and print_score is the method.
#-*-coding:utf-8-*
Class Student (object):
Def _ init_ _ (self,name,score):
Self.name= name
Self.score=score
Def print_score (self):
Print ('% s:% slots% (self. _ _ name,self. _ _ acore))
(3) instantiate the above class, and then analyze how Python uses the instantiated class to invoke methods in the class.
The complete instantiation class and calling procedure are as follows:
Pupil=student (name='lilei',score=99)
Pupil.print_score ()
According to the previous statement, self= instance name, then pupil=Student (name='lilei',score=99), the first step of instantiating a class is to execute the _ _ init_ _ statement, that is, Student (self='pupil',name='lilei',score=99), then pupil.name='lilei',pupil.score=9. The second step of instantiating the class is to execute the pupil.print_score () statement. The method to call the instance is to call the method of the class, which is printed out according to the format.
Pupil.name:pupil.score,mlilei: 99 .
3. The advantages of class
(1) access restrictions for classes.
The so-called access restriction of the class means that the data in the class cannot be called directly outside the class. In Python, adding "_ _" to the variable makes it private so that it can only be used in the class and cannot be called by functions (methods) other than the class. The name parameter in the following code represents a private variable.
#-*-coding:utf-8-*-
# _ _ author_ _ = 'Auntie N72'
Class Student (object):
Def _ init_ _ (self,name,score):
Self._ _ name= name
Self.score= score
Def print_score (self):
Print ('% s:% name,self._% (self._ _ name,self._ _ score))
MeiMei=Student ('hello', 99)
Print (MeiMei.score)
Print (MeiMei. _ _ name)
The running result of the code well proves that the parameter with "_ _" cannot be called, and the parameter without "_ _" can be called directly, as shown below:
C:\ Python36\ python3. Exe F:/Autotest_interface_demo/test. Py
ninety-nine
Traceback (most recent call last):
File "F:/Autotest interface demo/test.py", line 12, in
Print (MeiMei._name)
AttributeError: 'Student' object has no attribute'__name'
Process finished with exit code 1
(2) inheritance of classes.
The so-called "inheritance" is literally easy to understand: if a class inherits another class, then that class has all the methods and properties of the inherited class. We can use these methods and properties directly without having to write them separately. Here is an example to explain what inheritance is. The basic definition of the class is as follows:
#-*-coding:utf-8-*-
# _ _ author_ _ = 'Auntie N72'
# parent class
Class Parent (object):
Def print_self (self):
Return "I am the parent"
# subclasses inherit parent classes
Class Student (Parent):
Def_ _ init_ _ (self,name,score):
Self. _ _ name= name
Self.score=score
Def print_score (self):
Print ('% s:% slots% (self. _ _ name,self. _ _ score))
MeiMei=Student ('hello', 99)
Print (MeiMei.score)
# use the method of the parent class directly in the subclass
Print (MeiMei.print_self ()
In the above example, the subclass Student inherits the parent class Parent. The method is to add the name of the parent class in parentheses after the class name. If the subclass inherits the parent class, it has the method print_self of the parent class Parent, so it can be used directly in its own instance name.
(3) Polymorphism of class.
If the subclass inherits the parent class, but wants to change the method of the parent class, then there is no need to modify the method of the parent class, you only need to add the same method name to the subclass to play the role of overriding, this is polymorphism.
#-*-coding:utf-8-*-
# _ _ author_ _ = 'Auntie N72'
# parent class
Class Parent (object):
Def print_self (self):
Return "I am the parent"
# subclasses inherit parent classes
Class Student (Parent):
Def _ init_ _ (self,name,score):
Self. _ _ name= name
Self.score=score
Def print_score (self):
Print ('% s:% s% (self.) _ name,self. _ _ score))
Def print_self (self):
Return "I am a subclass"
MeiMei=Student ('hello', 99)
Print (MeiMei.score)
# methods of subclasses override parent methods
Print (MeiMei.print_self ())
Thank you for your reading, the above is the content of "what are the basic knowledge of Python class". After the study of this article, I believe you have a deeper understanding of what the basic knowledge of Python class has, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.