current position:Home>Python notes (13): operations such as object-oriented programming

Python notes (13): operations such as object-oriented programming

2022-01-30 12:38:19 A bowl week

Little knowledge , Great challenge ! This article is participating in “ A programmer must have a little knowledge ” Creative activities .

Hello everyone , I am a A bowl week , One doesn't want to be drunk ( Internal volume ) The front end of the . If you are lucky enough to get your favor , I'm very lucky ~

Understanding of operational concepts

operation (Operation) Is an abstraction of operational logic

  • Operation embodies an operation logic , In a broad sense, any program is an operation
  • Python The interpreter reserves a batch of operation interfaces through reservation methods , Overload required
  • The reserved method generally corresponds to the operator ,Python The operation in is embodied as the overloading of the operator
  • Operation essentially embodies the interaction 、 Include relationships and general operational relationships

Limitation of operation overload

  • Cannot overload Python Language built-in type operators
  • Cannot create a new operator , Can only be done by overloading
  • is,and,not,or Can't be overloaded

operator overloading

Arithmetic operator

  • Unary operator :+-~
  • Binary operator :+-*///%divmod()pow()**<<>>&^|
Keep the method Corresponding operation describe
.__neg__(self) -obj Define the operation logic of negative object
.__pos__(self) +obj Define the operation logic of positive object
.__abs__(self) abs(obj) Operation logic that defines the absolute value of an object
.__invert__(self) ~obj Define the operation logic of object negation
.__add__(self, other) obj + other The operation logic of adding two objects is defined
.__sub__(self, other) obj - other The operation logic of subtraction of two objects is defined
.__mul__(self, other) obj * other The operation logic of two object multiplication is defined
.__truediv__(self, other) obj / other The operation logic of two object division is defined
.__floordiv__(self, other) obj // other The operation logic of integer division of two objects is defined
.__mod__(self, other) obj % other The operation logic of two object modules is defined
.__divmod__(self, other) divmod(obj, other) The operation logic of dividing modules of two objects is defined
.__pow__(self, other) obj ** other Define the operation logic of object power
.__lshift__(self, other) obj << other Define the operation logic of moving the object to the left
.__rshift__(self, other) obj >> other Define the operation logic of moving the object to the right
.__and__(self, other) obj & other Define two objects located in the operation logic
.__xor__(self, other) obj ^ other Define the operation logic of bitwise exclusive or of two objects
.__or__(self, other) `obj other`

Comparison operator

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

Keep the method Corresponding operation
.__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

The operation overload of two object comparison operations

Members of the operation

  • Members get :[]def.reversed()
  • Member judgment :in
Keep the method Corresponding operation describe
.__getitem__(self, key) obj[k] Define the sequence number in the acquisition object K The operational logic of the element ,K Integers
.__setitem__(self, key, v) obj[k] = v Defines the sequence number in the assignment object K The operational logic of the element
.__delitem__(self, key) del obj[k] Defines the sequence number in the deleted object K The operational logic of the element
.__reversed__(self) obj.reversed() Define the operation logic of objects in reverse order
.__contains__(self, item) item in obj Definition in The operation logic corresponding to the operator

Other operations

Python Built in functions :rep(),str(),len(),int(),flaot,complex(),round(),bytes(),bool(),format(),.format( Common methods )

Keep the method Corresponding operation describe
__repr__(self) repr(obj) Define the operation logic of the printable string of the object
__str__(self) str(obj) Define the operation logic of object string conversion
__len__(self) len(obj) Define the operation logic of object length operation
__int__(self) int(obj) Define the operation logic of object integer conversion
__float__(self) float(obj) Define the operation logic of object floating-point conversion
__complex__(self) complex(obj) Define the operation logic of object complex conversion
__round__(self) round(obj) Define the operation logic of object rounding
__bytes__(self) bytes(obj) Define the operation logic of object byte string conversion
__bool__(self) bool(obj) Define the operation logic of object Boolean operation
.__format__(self, format_spec) obj.format() format(obj) Define the operation logic of object format output

Python Class polymorphism

polymorphic _(Polymorphism)_ Is for the method , Polymorphism that reflects the flexibility of the method ; To put it simply , It consists of two parts

  • Polymorphism of parameter types : The ability of a method to handle multiple types

    Python Function of / Methods have no type declaration restrictions , Naturally, it supports the polymorphism of parameter types

    Python The idea of programming is : Document constraints , Not syntactic constraints

    Distinction between different parameter types and functions , Need a programmer to complete

  • Parameter form polymorphism : The ability of a method to accept multiple parameters

    Python Function of / Method can support variable parameters , Support polymorphism in the form of parameters

    Python Class methods are also functions , Various definitions of functions are valid

    Determination of the number of different parameters and default values , It needs to be done by programmers

Polymorphism is OOP A traditional concept ,Python Nature supports polymorphism , No special grammar is needed , The sample code is as follows :

import abc
class Animal(metaclass=abc.ABCMeta): # The same thing : animal 
    @abc.abstractmethod
    def talk(self):
        pass

class Cat(Animal): # One of the forms of animals : cat 
    def talk(self):
        print('say miaomiao')

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

class Pig(Animal): # The third form of animals : The pig 
    def talk(self):
        print('say aoao')
 Copy code 

copyright notice
author[A bowl week],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201301238156908.html

Random recommended