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
The sidebar is recommended
- Some people say Python does not support function overloading?
- "Python instance" was shocked and realized the dirty words and advertisement detection of the chat system with Python
- Introduction to Python - CONDA common commands
- Python actual combat | just "4 steps" to get started with web crawler (with benefits)
- Don't know what to eat every day? Python to tell you! Generate recipes and don't worry about what to eat every day!
- Are people who like drinking tea successful? I use Python to make a tea guide! Do you like it?
- I took 100g pictures offline overnight with Python just to prevent the website from disappearing
- Binary operation of Python OpenCV image re learning and image smoothing (convolution processing)
- Analysis of Python event mechanism
- Iterator of Python basic language
guess what you like
-
Base64 encryption and decryption in Python
-
Chapter 2: Fundamentals of python-2 variable
-
Python garbage collection summary
-
Python game development, pyGame module, python takes you to realize a magic tower game from scratch (1)
-
Python draws a spinning windmill with turtle
-
Deep understanding of Python features
-
A website full of temptations for Python crawler writers, "lovely picture network", look at the name of this website
-
Python opencv Canny edge detection knowledge supplement
-
Complex learning of Python opencv Sobel operator, ScHARR operator and Laplacian operator
-
Python: faker extension package
Random recommended
- Python code reading (Part 44): find the location of qualified elements
- Elegant implementation of Django model field encryption
- 40 Python entry applet
- Pandas comprehensive application
- Chapter 2: Fundamentals of python-3 character string
- Python pyplot draws a parallel histogram, and the x-axis value is displayed in the center of the two histograms
- [Python crawler] detailed explanation of selenium from introduction to actual combat [1]
- Curl to Python self use version
- Python visualization - 3D drawing solutions pyecharts, Matplotlib, openpyxl
- Use python, opencv's meanshift and CAMSHIFT algorithms to find and track objects in video
- Using python, opencv obtains and changes pixels, modifies image channels, and trims ROI
- [Python data collection] university ranking data collection
- [Python data collection] stock information collection
- Python game development, pyGame module, python takes you to realize a magic tower game from scratch (2)
- Python solves the problem of suspending execution after clicking the mouse in CMD window (fast editing mode is prohibited)
- [Python from introduction to mastery] (II) how to run Python? What are the good development tools (pycharm)
- Python type hints from introduction to practice
- Python notes (IX): basic operation of dictionary
- Python notes (8): basic operations of collections
- Python notes (VII): definition and use of tuples
- Python notes (6): definition and use of lists
- Python notes (V): string operation
- Python notes (IV): use of functions and modules
- Python notes (3): conditional statements and circular statements
- Python notes (II): lexical structure
- Notes on python (I): getting to know Python
- [Python data structure series] - tree and binary tree - basic knowledge - knowledge point explanation + code implementation
- [Python daily homework] Day7: how to combine two dictionaries in an expression?
- How to implement a custom list or dictionary in Python
- 15 advanced Python tips for experienced programmers
- Python string method tutorial - how to use the find() and replace() functions on Python strings
- Python computer network basics
- Python crawler series: crawling global airport information
- Python crawler series: crawling global port information
- How to calculate unique values using pandas groupby
- Application of built-in distribution of Monte Carlo simulation SciPy with Python
- Gradient lifting method and its implementation in Python
- Pandas: how to group and calculate by index
- Can you create an empty pandas data frame and fill it in?
- Python basic exercises teaching! can't? (practice makes perfect)