In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to understand the concept of class in Python object-oriented programming". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. please follow the editor's train of thought to study and learn "how to understand the concept of class in Python object-oriented programming".
1. Object-oriented basic concept 1.1 everything is an object
In Python language, all data types are objects, functions are objects, and modules are objects.
Python all classes are inherited from the most basic class object
The operation function of data type in Python language is the embodiment of class method.
1.2 object-oriented programming
Object-oriented programming is also called OOP (Object-Oriented-Programming).
OOP: object-oriented programming, a programming idea that focuses on highly abstract reused code
OOP regards the object as the basic unit of the program, and the object contains data and functions that manipulate the data.
The essence of OOP is to abstract problem solving into an object-centered computer program.
OOP is very useful in large-scale or complex projects, and OOP can increase collaboration productivity
The main value of OOP lies in code reuse.
OOP is just a programming method, not an advanced way to solve problems.
The difference between process-oriented and object-oriented
Process-oriented programming with problem-solving process steps as the core, object-oriented programming with problem object construction and application as the core, all problems that can be solved by OOP can be solved by process-oriented.
1.3 object-oriented features
Encapsulation: abstraction of properties and methods, using data and methods to manipulate data to form object logic
Method abstraction: define, isolate, and protect the properties (variables) of a class
Abstraction of objects: defining, isolating, and protecting methods (functions) of a class
The goal is to form an interface of a class to external operable properties and methods
Inheritance: a high-level abstraction of code reuse that uses inheritance relationships between objects to form code reuse
Inheritance is one of the quintessence of object oriented programming
It implements code reuse at a high level of abstraction in class units.
Inheritance is a process in which a newly defined class can almost completely use the properties and methods of the original class.
Polymorphism: the abstraction of method flexibility, which makes the operation of objects more flexible and more reusable code.
Polymorphism of parameter types: the ability of a method to handle multiple types
Polymorphism in the form of parameters: the ability of a method to accept multiple parameters
Polymorphism is a traditional concept of OOP. Python naturally supports polymorphism and does not require special syntax.
2. Python object-oriented terminology
Classes (Class) and objects (Object)
Classes: templates for logical abstraction and generation of objects, specific choreography of a set of variables and functions
Object: an entity that specifically expresses data and operations, equivalent to "variables" in a program. Including: class object, instance object
After the class definition is completed, a class object is generated by default, and each class uniquely corresponds to a class object. The class object used to store this tired basic information is an instance of type, expressed as a type type.
Instance object (Instance Object): the object generated after the instance of the Python class, abbreviated as object
There is only one class object globally, and multiple instance objects can be generated.
Attribute: a "variable" that stores data (that is, a variable defined in a class) that describes some of the property parameters of the class. Including: class properties, instance properties
Class Attribute: the property of a class object, shared by all instance objects; defined within the class, outside the _ _ init__ function. Generally, the properties common to a class are defined as class properties.
Instance property (Instance Attribute): the property of an instance object, which is generally defined in a function in the class. The instance property may be unique to an instance.
Method: the "method" of manipulating the data (that is, the variables defined in the class), which is used to give the operation function of the class. Including: class method, instance method, free method, static method, reserved method
Class method (Class Method): a method of a class object, shared by all instance objects
Instance method (Instance Method): the method of an instance object, which is exclusively enjoyed by each instance object, the most commonly used form,
Free method (Namespace Method): a normal function in a class that is managed by the namespace in which the class resides and is exclusive to the class object.
Static method (Static Method): a common function in a class that is shared by objects and instance objects
Retention method (Reserved Method): there are double underlined Kashgar and end methods, reserved for use.
Instantiation: from class to object, all "objects" are derived from a "class"
3. The construction of Python class 3.1 basic construction of class
In Python, we use the class keyword plus the class name to define the class, and we can determine the code block of the class by indenting it, just as we would define the function. Grammatical structure
Class: [class description "documentation string"]
Because Python is a scripting language, the definition class has no limited location and can be included in branches or other subordinate statements, and execution can be present.
Class name: can be any valid identifier, usually with uppercase initials
Class description: the first line after the class definition is defined as an independent string; after definition, it is accessed through. _ _ doc__
Sample code
Class TestClass: "" this is a test class "" print ("Hello Class Object") print (TestClass.__doc__) print (type (TestClass))''- output results-Hello Class Object this is a test class''
Statements directly contained in class objects will be executed, and all defined classes will not contain statements directly in the class as far as possible.
Instance object: instance object is the most commonly used method of Python class
Create instance object syntax structure
= ([parameter])
Example code:
Class TestClass: print ("one bowl week") tt = TestClass () print (type (tt))'- output results-one bowl week''3.2 class constructor
Concept:
The process by which the constructor of a class is used to create an instance object from a class
The constructor of the class provides a parameter input method for instance object creation.
The constructor of the class supports the definition and assignment of instance properties.
The predefined _ _ init__ () is used as the constructor in Python
Grammatical structure:
Class ClassName: def _ init__ (self, [Parameter 1], [Parameter 2],... [parameter n]):.
Example code:
Class TestClass: def _ init__ (self, name): print (name) text1 = TestClass ("Zhang San") # Zhang San text2 = TestClass ("Li Si") # Li Si
You can provide parameters to Python through the constructor _ _ init__
Instructions for using * * _ _ init__ () * *
Parameters: the first parameter convention is self, which represents the class instance itself, and the other parameters are instance parameters (self is used internally and retained by default, and parameters entered by other users are placed after self)
Function name: defined internally by the Python interpreter, starting and ending with a double underscore (_ _)
Return value: the constructor does not return a value, or returns None, otherwise a TypeError exception is generated.
* * self** represents an instance of a class within the class definition
Slef is a class parameter agreed upon in Python object-oriented.
Self represents an instance of a class. Within the class, self is used to combine properties and methods related to accessing the instance.
By contrast, the class name represents the class object itself
3.3 attributes of CLA
Properties are variables defined within the class, and the syntax structure is as follows:
Class ClassName: = def _ init__ (self, [parameter 1], [parameter 2],... [parameter n]): self. =.
Access class properties:. Or.
Access instance properties:.
Instance code
Class TestClass: count = 0 # class attribute def _ init__ (self, name, age): self.name = name # instance attribute self.age = name TestClass.count + = 1 # instantiate once count+1students1 = TestClass ("Zhang San", "18") students2 = TestClass ("Li Si", "19") print ("Total:", TestClass.count) # Total: 2print (students1.name Students2.name) # methods of Zhang San Li Si 3.4
(1) instance method: the instance method is a function defined within the class, independent of the instance object and grammatical structure.
Class ClassName: def (self,):...
() the definition of the instance method the first parameter is self
Instance code
Class TestClass: def _ _ init__ (self Number): self.number = number def sum_number (self): # instance method sum_num = 0 for i in range (self.number + 1): # because the loop will not reach the last number sum_num + = I return sum_numnumber1 = TestClass (100) number2 = TestClass (10) print (number1.sum_number ()) # 5050print (number2.sum_number ()) # 55
(2) Class methods: class methods are functions related to class objects, all instance objects are shared, and the syntax structure ↓
Class ClassName: @ classmethod def (cls,):...
() or. (). The class method contains at least one parameter that represents the class object. It is recommended that @ classmethod is the decorator, which is necessary for the class method definition.
Class methods can only manipulate class properties and other class methods, not instance properties and instance methods.
Instance code
Class TestClass: sum_num = 0 def _ _ init__ (self Number): self.number = number def sum_number (self): for i in range (self.number + 1): # because the loop will not reach the last number TestClass.sum_num + = I return TestClass.sum_num @ classmethod def test (cls): # class method test_value = TestClass.sum_num * 2 return test_valuevalue1 = TestClass (100) print (value1.sum_number ()) # 5050print (value1.test ()) # 10100
(3) own method: its own method is an ordinary function defined in the class name space. The syntax format is as follows:
Class ClassName: def ():...
. (), which represents the namespace. Native methods do not require parameters such as self or cls and can have no parameters. Self-owned methods can only operate on class properties and class methods, not instance properties and instance methods. Free methods can only be used by using the
Strictly speaking, a free method is not a method, but a function defined in a class.
Instance code
Class TestClass: sum_num = 0 def _ _ init__ (self Number): self.number = number def sum_number (self): for i in range (self.number + 1): # because the loop will not reach the last number TestClass.sum_num + = I return TestClass.sum_num def test (): # Free method test_value = TestClass.sum_num * 2 return test_valuedef test_out (): # and so on In the free method above test_out_value = TestClass.sum_num * 2 return test_out_valuevalue = TestClass (100) print (value.sum_number ()) # 5050print (TestClass.test ()) # 10100print (test_out ()) # 10100
Free methods in a defined class can also be defined outside.
(4) static method: a common function defined in a class that can be shared by all instance objects.
Class ClassName: @ staticmethod def ():...
. () or. () static methods can also be used without parameters, which can be understood as their own methods that can be called using the object name. @ staticmethod is a decorator, static method definition is necessary, static methods are the same as free methods, can only operate on class properties and methods, can not operate instance properties and instance methods, the difference is that you can pass or.
Sample code
Class TestClass: sum_num = 0 def _ _ init__ (self Number): self.number = number def sum_number (self): for i in range (self.number + 1): # because the loop will not reach the last number TestClass.sum_num + = I return TestClass.sum_num @ staticmethod # static method decorator def test (): test_value = TestClass.sum_num * 2 return test_valuevalue = TestClass Print (value.sum_number ()) # 5050print (TestClass.test ()) # 10100print (value.test ()) # 10100
(5) retention method: there are double underlined Kashgar and ending methods, which are retained and used. Grammatical structure ↓
Class ClassName: def ():...
Reserved methods generally correspond to some operation of the class, and the method is called when the operator is used, which is retained in the Python interpreter.
Thank you for your reading, the above is the content of "how to understand the concept of class in Python object-oriented programming". After the study of this article, I believe you have a deeper understanding of how to understand the concept of class in Python object-oriented programming. 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.