Python Object Oriented Programming - Class Objects.
Class object
When creating a class.
class class name :
Actually, when
interpreter When using the class statement, a class object will be created (everything in Python is an object)
Test code:
class Student:
pass #empty statement
print(type(Student))
print(id(Student))
stu = Student
s1 =stu()
print(s1)
Running results:
<class 'type'>
1596129631944
<__main__.Student object at 0x00000173A2A73518>
We can see that an object with the variable name "Student" was actually generated. By assigning a value to the new variable "stu", we can also make related calls. This indicates that a "class object" was indeed created.
Class Properties
Class attributes are attributes that belong to class objects, also known as class variables. As class attributes belong to class objects, they can be shared by all instance objects.
How to define class attributes:
class class name :
class variable name = initial value
Within or outside of a class, we can read and write it using "class name. class variable name".
Class method
Class methods are methods that belong to "class objects". Class methods are decorated with a decorator; Classmethod is defined in the following format:
@classmethod
def class method name( cls [ , formal parameter list ]) :
function body
The key points are as follows:
1. @Classmethod must be on the line above the method
2. The first cls must have; CLS refers to the "class object" itself<3. Calling class method format: "class name. class method name (parameter list)". In the parameter list, CLS does not need or cannot be passed values<4. Accessing instance properties and methods in class methods can lead to errors. 5. When a subclass inherits a parent class method, passing cls is a subclass object, not a parent class object.
Static method
1. In Python, it is allowed to define methods that are independent of class objects, known as "static methods"<2. There is no difference between "static methods" and defining ordinary functions in modules, except that "static methods" are placed in the "class namespace" and need to be called through "class calls" The key points are as follows: 1. @Staticmethod must be on the line above the method Static method usage testing. Instance properties are properties that belong to instance objects, also known as "instance variables". Their use has the following key points: 1. In other instance methods of this class, access is also done through self: self. Instance property name 1. An instance method is a method that belongs to an instance object. The definition format of an instance method is as follows: The calling format of the method is as follows: Key point: 3. The essence of method calls for instance objects:
@staticmethod
def static method name( [ parameter list ]) :
function body
2. Calling static method format: "class name. static method name (parameter list)"class Student:
company = "SXT" #class properties
@staticmethod
def add(a, b): #static method
print("{0}+{1}={2}".format(a,b,(a+b)))
return a+b
Student.add(20,30)
Instance Properties
1. Instance properties are generally used in__Init__The () method is defined by the following code: self. instance property name = initial value
2. After creating an instance object, access is done through the instance object:
obj01= Class Name () # Create Object, Call__Init__() Initialize Property
obj01. Instance Property Name= Value # can assign values to existing attributes or add new attributes.Example Method
def method name( self [ , formal parameter list ]):
function body
object . method name( [ argument list ])
1. When defining an instance method, the first parameter must be self. As before, self refers to the current instance object