Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. Object Oriented Programming (OOP) requires “classes” and “objects” to implement, so object-oriented programming is the use of “classes” and “objects”.

Brief introduction

Procedure oriented:

Write code top-down according to the program logic.

Functional:

Encapsulate the code for some functionality into functions, and afterwards there is no need to write it again — just call the function. This generally suits scenarios where the functions are independent and share no data.

Object oriented:

Classify and encapsulate functions.

About classes and objects

  • A class is a template, and the template contains many functions, each implementing some functionality
  • An object is an instance created from the template; through the object you can execute the functions in the template
#create a class
class A:

    def Func(self):
        print("Afun")

    def Hello(self,name):
        print("Hello,%s"%name)

#create object obj from class A
obj=A()
obj.Func  #call the Func method
obj.Hello('Alex')  #call the Hello method

The first parameter of a function in a class must be self (it is the reference to the object itself);

Functions defined in a class are called “methods”.

The three major features of object orientation

The three major features of object orientation are: encapsulation, inheritance and polymorphism.

Encapsulation

Encapsulation means hiding the working details of an object from the outside world. When using the encapsulation feature of object orientation, you need to:

  • Encapsulate the content somewhere
  • Call the encapsulated content from somewhere
class Foo:
    #__init__ is called the constructor and is executed automatically when an instance of the class is created
    def __init__(self,name,age):
        self.name=name
        self.age=age

obj=Foo('Alex',20)  #encapsulate Alex and 20 in the name and age attributes of obj/self

There are two ways to call the encapsulated content:

  1. Call it directly through the object
  2. Call it indirectly through self
class Foo:
    def __init__(self,name,age):
        self.name=name
        self.age=age

    def Detail(self):
        print(self.name)
        print(self.age)

obj=Foo('Alex',20)
print(obj.name)  #directly call the name attribute of the obj object
obj.Detail()  #indirectly call the attributes of the obj object

In summary, for object-oriented encapsulation, what happens is that the constructor is used to encapsulate the content into the object, and then the encapsulated content is obtained either directly through the object or indirectly through self.

Inheritance

Build specialized class objects on the basis of a general class

class Animal():
    def eat(self):
        pass

    def drink(self):
        pass

    def shit(self):
        pass

    def pee(self):
        pass

class Cat(Animal):
    def sound(self):
        print("Meow")

class Dog(Animal):
    def sound(self):
        print("Woof")

From the code above we can see that both the Cat and Dog classes take Animal as their base class, so both Cat and Dog have the methods of the Animal class; this is inheritance.

Multiple inheritance

  1. A Python class can inherit from multiple classes
  2. If a Python class inherits from multiple classes, there are two ways of looking things up: depth first and breadth first
    • When the class is an old-style class, in the multiple inheritance case the lookup follows the depth-first manner
    • When the class is a new-style class, in the multiple inheritance case the lookup follows the breadth-first manner (When a class or its parent class inherits from the object class, the class is a new-style class, otherwise it is an old-style class)
class A():
    def foo(self):
        print('A.foo')

class B(A):
    def foo(self):
        print('B.foo')

class C(A):
    def foo(self):
        print('C.foo')

class D(B,C):
    def foo(self):
        print('D.foo')

k=D()

Old-style class: first look in class D; if not found in D, continue to look in class B; if not found in B, continue to look in class A; if not found in A, continue to look in class C; if still not found, raise an error.

New-style class: first look in class D; if not found in D, continue to look in class B; if not found in B, continue to look in class C; if not found in C, continue to look in class A; if still not found, raise an error.

In the process above, once something is found the lookup stops immediately and does not continue.

Polymorphism

Polymorphism means that the same operation can be applied to objects of different classes.

class Foo:
    pass

class S1(Foo):
    def show(self):
        print('S1.show')

class S2(Foo):
    def show(self):
        print('S2.show')

def Func(obj):
    print(obj.show)

s1_obj=S1()
Func(s1_obj)  #pass an object of class S1 into Func(), which executes the show method of class S1

s2_obj=S2()
Func(s2_obj)  #pass an object of class S2 into Func(), which executes the show method of class S2

Summary

  • The above is an introduction to the object-oriented programming style; this style is implemented on the basis of using classes and objects
  • A class is a template, and the template packages multiple functions for use
  • An object is an instance created from the template, and the instance is used to call the functions packaged in the class
  • Object orientation has three features: encapsulation, inheritance and polymorphism

Aside

How classes and objects are stored in memory

A class and the methods in the class exist only once in memory, whereas every object created from the class needs its own copy in memory, because the values of methods or variables may differ between objects, and between an object and its base class. When an object is created from a class, in addition to encapsulating the values of its own variables, the object also stores a pointer to a class object, which points to the class of the current object.