What is python class study notes

classes and objects

class definition

insert image description here
①, class: various types, such as electronics, furniture, clothing, food...;
②, instance: an instance of a class is called an instance, which can be understood as a "practical example".

class creation and calling

insert image description here

class creation

①. Class creation: class+class name+colon
to automatically detect
class leiming():
pass #Remember to indent here
②. Class property creation:
automatic detection through assignment statement
#The following a is variable
a=1
#The following a is Properties of class A()

class A():
a=1

③. Creation of instance methods: def + method name (self) (emphasis: self represents the instantiated object)
automatic detection
#The following B() is a function
def B():
pass
#The following B() is a class A( ) method B()

class A():
def B(self):
pass

class call【】

For example:

class teacher():
name=‘延君’
def work(self):
print(‘learning ~~~’)
t=teacher()
print(t.name)
t.work()

①, line 5 "t=teacher()" refers to the instantiated class teacher, where t is the instantiated object (that is, self = t); ②, line 6 "print(
t.name)" refers to calling the instantiated object Attribute name;
③. Line 7 "t.work()" refers to calling the method work of the instantiated object.

Key Points of Class Creation

self (represents the instantiated object itself)

self is the substitute of all instances (this is a substitute attack!!!)
①, when calling other methods inside the method of the class, we also need to use self to represent the instance

class A():
def B(self):
print(self)
a=A()
a.B()

After the above code is executed, the output of the terminal is < main .A object at 0x000001B29C851080> , that is, our self represents the instantiated object a of class A.
②. When using def to create a method in a class, you must leave the position of the first parameter to self, and ignore it when calling the method (no need to pass parameters to self) ③. When you want to call
class attributes or other methods inside a class method , it is necessary to use the format of self. attribute name or self. method name

class A():
name='Yanjun'
def B(self):
print('The assistant's name is'+self.name)
a=A()
aB()

initialization method

①. Format definition: def init (self), which is composed of init plus [double] underscores on the left and right sides
②. Function: When each instance object is created, the code in this method will run automatically without calling

class A():
def init (self):
print('I am the most handsome teaching assistant, none of them')
a=A()

The above initialization functions do not need to be called, and run directly once instantiated.
③Using this feature, in the writing habit, we will complete the creation of class attributes inside the initialization method, and set the initial value for the class attributes, so that other methods in the class can be called directly and at any time

Key knowledge points of the class

self function call

class A():
def Name(self):
print('I am the most handsome teaching assistant {}, none of them'.format(self.name))
def Work(self):
print('My favorite class reminder')
a=A()
a.name='Yanjun'
a.Name()
a.Work()

In the above code, starting from line 6, we instantiate the class A and create an instance object a, create an attribute name for the instantiated object a and assign the value 'Yanjun'. After we call the Name method in the class, our statement is printed out.

initialization method init function

class A():
def init (self,name):
self.name=name
def Day(self):
print('Today is the third day of reminder for {}'.format(self.name))
a=A ('Yanjun')
a.Day()

In the above code, we instantiated class A and assigned the value 'Yanjun'. Because it is instantiated, the initialization function init is executed by default, and 'Yanjun' is given to the attribute name of the instantiated object a, so that we call the method On Day, the normal output is "Today is the third day of Yanjun's class reminder".
Here self.name=name It should be noted that self.name is the name of the instantiated object, and the latter is the parameter name in the function, and the positioning of the two is different. That is, we can write it as self.mingzi = name, then we need to write it as self.mingzi when we call it.

__str__ function (knowledge expansion)

class A():
def init(self,name):
self.name=name
def str(self):
return ‘我是{}’.format(self.name)
a=A(‘延君’)
print(a)

In python, if the method name is __xxxx__(), then it has a special function, so it is called a "magic" method. The __str__ method needs to return a string, which is used as a description of this object, and it is also executed after instantiation. Methods.

Guess you like

Origin blog.csdn.net/qq_44992918/article/details/109341691