Python learning diary day30

Content: Object Oriented Programming

Three characteristics of object-oriented:
Encapsulation
Hide the details of the object and only provide the necessary methods. It is equivalent to blocking the details and exposing only the relevant calling methods.
Inherit
Inheritance can make subclasses have the characteristics of parent classes and improve the importance of code.
Polymorphism
Polymorphism means that the same method is used for different objects to produce different behaviors


inherit
Inheritance is not only an important feature of object-oriented programs, but also an important means to realize 'code reuse'.
If a new class inherits from a designed class, it will directly have the characteristics of the existing class, which greatly reduces the difficulty of work.
Syntax format:
Python supports multiple inheritance. A subclass can inherit multiple parent classes. The inherited syntax format is as follows:
Class subclass class name (parent class 1 [, parent class 2, parent class 3...])
Class body
If no parent class is specified in the class definition, the default parent class is the object class, that is, object is the parent class of all classes, which defines some default implementations common to all classes, such as:__ new__ ().

The definition of a subclass is that the constructor of the parent class must be called in its constructor. The calling format is as follows:
Parent class name__ init__(self, parameter list)

# Basic use of test inheritance
class Person:
    def __init__(self, name, age):
        self.name = name
        self.__age = age

    def say_age(self):
            print('full name:{0}\t Age:{1}'.format(self.name, self.__age))


class Student(Person):
    def __init__(self, name, age, score):
        self.__score = score
        Person.__init__(self, name, age)
a= Student('ww',18,90)
a.say_age()
print(a.name)
print(dir(a))
print(a._Person__age)
###result:
full name: ww	Age: 18
ww
['_Person__age', '_Student__score', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'name', 'say_age']
18

When the parent class contains a private attribute, if you want to call the private attribute, the format is as follows: a_ Person__ age

Inheritance and override of class members:
1. Member inheritance: the subclass inherits all members of the parent class except the constructor.
2. Method Rewriting: subclasses can redefine the methods in the parent class, and the methods inherited by the parent class will be overwritten.

View the inheritance hierarchy of a class
Through class method MRO () or class attribute__ mro__ You can output the inheritance hierarchy of this class.
 

class A:
    pass
class B(A):
    pass
class C(B):
    pass
print(C.mro())
###result:
[<class '__main__.C'>, <class '__main__.B'>, <class '__main__.A'>, <class 'object'>]

object root class:
Object class is the parent class of all classes, so all classes have the properties and methods of object class.

dir() view object properties:
 

class Person:
    def __init__(self, name, age):
        self.name = name
        self.__age = age

    def say_age(self):
            print('full name:{0}\t Age:{1}'.format(self.name, self.__age))

obj=object()
s=Person('ww',19)
print(dir(obj))
print(dir(s))
###result:
['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
['_Person__age', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'name', 'say_age']

Object has a__ str__ The () method is used to return a description of the object, corresponding to the built-in function str (). It is often used in the print() method to help view the information of the object__ str__ () can be overridden.

Multiple inheritance:
Python supports multiple inheritance. A subclass can have multiple direct parent classes. Try to avoid using!!!


mor (method resolution order): method resolution order. We can obtain the 'class hierarchy' through the mro () method, and the method parsing order is also found according to the 'class hierarchy'.
 

class A:
    def aa(self):
        print('aa')
    def cc(self):
        print('aaaa')
class B:
    def bb(self):
        print('bb')
    def cc(self):
        print('bbbb')
class C(B,A):
    def ccc(self):
        print('cc')

c=C()
c.cc()
c.ccc()
##result:
bbbb
cc

super() gets the definition of the parent class
In a subclass, if you want to get the method of the parent class, you can use super ()
super () represents the definition of the parent class, not the parent object
 

#Test super()
class A:
    def say(self):
        print('A:',self)

class B(A):
    def say(self):
        super().say()
        print('B:',self)

B().say()
#result:
A: <__main__.B object at 0x0000021C18902830>
B: <__main__.B object at 0x0000021C18902830>


Polymorphism:
Polymorphism means that the same method call may have different behaviors due to different objects:

class Person:
    def celebrate(self):
        print('Happy holidays')
class Chinese(Person):
    def celebrate(self):
        print('Japan's return to the embrace of the motherland')
class American(Person):
    def celebrate(self):
        print('Comrade Chuan Jianguo stepped down')
def Celebrate(m):
    if isinstance(m,Person):
        m.celebrate()
    else:
        print('The world celebrates the end of COVID-19.')
Celebrate(Chinese())
Celebrate(American())
class Japanese:
    pass
Celebrate(Japanese())
#result:
Japan's return to the embrace of the motherland
 Comrade Chuan Jianguo stepped down
 The world celebrates the end of COVID-19.

Summary:

I didn't expect that I could punch in for 30 days at the beginning. After all, we know what virtue we have learned in these 30 days. Then, we really have a foundation. It's obvious that we have encountered a great bottleneck - we have determined a project, clearly defined the goal and realized the function, but it's easy to mix up or think too simply, Not comprehensive. I also try to write the project process with brain map and flow chart, but I can't get the focus of a project or some necessary steps to realize that function. For the time being, most of the items updated later should be projects written by others. The main purpose is to learn this thinking. I think I have a foundation of python. Even if my comprehension is poor, I can be regarded as an entry-level. The future can be expected!!! No problem.

Keywords: Python Back-end

Added by dustinkrysak on Mon, 10 Jan 2022 03:16:10 +0200