Operations such as Python object oriented programming

  • 2021-12-11 18:37:43
  • OfStack

Directory 1, Understanding of Operation Concept 2, Operator Overloading 2.1 Arithmetic Operator 2.2 Comparison Operator 2.3 Member Operation 2.4 Other Operation 3, Python Class Polymorphism

1. Understanding the concept of operation

Operation ( Operation ) is an abstraction of operational logic

Operation embodies one kind of operation logic, and any program is one kind of operation in a broad sense Python The interpreter reserves the interface of 1 batch operation through the reservation method, which needs to be overloaded Keep the operator corresponding to Method 1, Python The operation in is embodied as the overload of the operator Operation essentially embodies interaction relation, inclusion relation and conventional operation relation

Limitation of operation overload

Cannot overload Python Operators of built-in types in the language You cannot create a new operator, it can only be done by overloading is , and , not , or Can't be overloaded

2. Overloading of operators

2.1 Arithmetic Operators

1 yuan operator: + , - , Python0
2 yuan operator: + , - , * , / , // , % , divmod()  , pow() , ** , << , >> , & , ^ , |

保留方法 对应操作 描述
.__neg__(self) -obj 定义对象取负的运算逻辑
.__pos__(self) +obj 定义对象取正的运算逻辑
.__abs__(self) abs(obj) 定义对象绝对值的运算逻辑
.__invert__(self) ~obj 定义对象取反的运算逻辑
.__add__(self, other) obj + other 定义了两个对象加法的运算逻辑
.__sub__(self, other) obj - other 定义了两个对象减法的运算逻辑
.__mul__(self, other) obj * other 定义了两个对象乘法的运算逻辑
.__truediv__(self, other) obj / other 定义了两个对象除法的运算逻辑
.__floordiv__(self, other) obj // other 定义了两个对象整数除的运算逻辑
.__mod__(self, other) obj % other 定义了两个对象模的运算逻辑
.__divmod__(self, other) divmod(obj, other) 定义了两个对象除模的运算逻辑
.__pow__(self, other) obj ** other 定义对象幂的运算逻辑
.__lshift__(self, other) obj << other 定义对象左移的运算逻辑
.__rshift__(self, other) obj >> other 定义对象右移的运算逻辑
.__and__(self, other) obj & other 定义两个对象位于运算逻辑
.__xor__(self, other) obj ^ other 定义两个对象位异或的运算逻辑
.__or__(self, other) `obj other`

2.2 Comparison Operators

Comparison operator: <、<=、==、!=、>、>=

保留方法 对应操作
.__lt__(self, other) obj < other
.__le__(self, other) obj <= other
.__eq__(self, other) obj == other
.__be__(self, other) obj != other
.__gt__(self, other) obj > other
.__ge__(self, other) obj >= other

Operational overload of two object comparison operations

2.3 Member operations

Member Acquisition: [] , def , .eversed()
Member judgment: in

保留方法 对应操作 描述
.__getitem__(self, key) obj[k] 定义获取对象中序号K元素的运算逻辑,K为整数
.__setitem__(self, key, v) obj[k] = v 定义赋值对象中序号K元素的运算逻辑
.__delitem__(self, key) del obj[k] 定义删除对象中序号K元素的运算逻辑
.__reversed__(self) obj.reversed() 定义对象逆序的运算逻辑
.__contains__(self, item) item in obj 定义in操作符对应的运算逻辑

2.4 Other operations

Python Built-in functions: Python1 (Common Methods)

保留方法 对应操作 描述
__repr__(self) repr(obj) 定义对象可打印字符串的运算逻辑
__str__(self) str(obj) 定义对象字符串转换的运算逻辑
__len__(self) len(obj) 定义对象长度操作的运算逻辑
__int__(self) int(obj) 定义对象整数转换的运算逻辑
__float__(self) float(obj) 定义对象浮点数转换的运算逻辑
__complex__(self) complex(obj) 定义对象复数转换的运算逻辑
__round__(self) round(obj) 定义对象4舍5入的运算逻辑
__bytes__(self) bytes(obj) 定义对象字节串转换的运算逻辑
__bool__(self) bool(obj) 定义对象布尔运算的运算逻辑
.__format__(self, format_spec) obj.format() format(obj) 定义对象格式化输出的运算逻辑

3. Polymorphism of Python class

Polymorphism _ ( Python2 ) _ is a polymorphism aiming at the method and embodying the flexibility of the method; Simply put, it consists of two parts

Polymorphism of parameter types: the ability of a method to handle multiple types

Python The function/method of has no type declaration restriction and naturally supports polymorphism of parameter types

Python The programming philosophy is: document constraints, not syntax constraints

The distinction and function of different parameter types need to be completed by programmers

Polymorphism in parameter form: the ability of a method to accept multiple parameters

Python Functions/methods of can support variable parameters and polymorphism in the form of parameters

Python Class methods of are also functions, and various definitions of functions are valid

It is necessary for programmers to determine the number of different parameters and their default values

Polymorphism is a traditional concept of OOP. Python naturally supports polymorphism without special syntax. The example code is as follows:


import abc
class Animal(metaclass=abc.ABCMeta): # Same as 1 Similar thing : Animals 
    @abc.abstractmethod
    def talk(self):
        pass

class Cat(Animal): # The form of animals 1: Cat 
    def talk(self):
        print('say miaomiao')

class Dog(Animal): # The form of animals 2: Dog 
    def talk(self):
        print('say wangwang')

class Pig(Animal): # The form of animals 3: Pig 
    def talk(self):
        print('say aoao')

Related articles: