Magic methods and uses of Python

In Python, all methods that are double-underwritten with "_ _" are called "magic methods"
Magic method Python interpreter automatically gives the default, so unless you need to change its internal function, other times use the default magic method

The three most commonly used: "init", "_new", "del_"

_* new is used to create a class and return an instance of this class. ** _init * will pass in parameters to initialize the instance and initialize the sample properties, forming a "constructor" together with new
* del* destroy the instantiated object, which is the destructor

Class call: call

* call* allows a class to be called like a function

Attribute access: getattr, setattr, delattr

_* This method is called when getattr accesses the non-existent attributes of the object to define the access behavior ** _setattr* is called when the object attributes are set
* delattr* called when deleting object attributes

Context manager: _enter and _exit

For these two methods, please see question 3 above.

Iterator methods: iter and \__next__

* iter: returns a container iterator, in many cases it will return an iterator, especially when the built-in iter() method is called, and when using for x in container: loop. Iterators are their own objects, and they must define an iter method that returns self.
_
next_*: returns the next element of the iterator

Recommended Posts

Magic methods and uses of Python
Python commonly used magic methods
Mongodb and python interaction of python crawler
Python implementation of intersection and IOU tutorial
Example of python calculating derivative and plotting
Analysis of common methods of Python multi-process programming
7 features of Python3.9
Python and Go
Python realizes horizontal and vertical splicing of pictures
The difference between the syntax of java and python
The meaning and usage of lists in python
Analysis of common methods of Python operation Jira library
Detailed explanation of how python supports concurrent methods
Python uses PIL to cut and stitch pictures
Python introspection and reflection
Python magic method topic
Python object-oriented magic method
A brief summary of the difference between Python2 and Python3
Pycharm uses embedded Python
Solve the problem of python compiling and installing ssl
Python drawing | A variety of typhoon path visualization methods
Installation and use of GDAL in Python under Ubuntu
[python] python2 and python3 under ubuntu
Basics of Python syntax
Basic syntax of Python
Basic knowledge of Python (1)
Prettytable module of python
Python deconstruction and packaging
Python3 configuration and entry.md
09. Common modules of Python3
Python black magic metaclass
Python automated operation and maintenance 2
Consolidate the foundation of Python (4)
Python introduction and environment installation
Consolidate the foundation of Python(7)
Python know crawler and anti crawler
In-depth understanding of python list (LIST)
Subscripts of tuples in Python
Python analysis of wav files
centos7 install python3 and ipython
Consolidate the foundation of Python(6)
ubuntu18.04 compile and install python3.8
Analysis of JS of Python crawler
python king of glory wallpaper
Centos 6.10 reinstall python and yum
Python open read and write
Consolidate the foundation of Python(5)
Python implementation of gomoku program
Analysis of Python Sandbox Escape
Some new features of Python 3.10
CentOS7 install python3 and pip3
Deep understanding of Python multithreading
Python automated operation and maintenance 1
Python data structure and algorithm
Python magic function eval () learning
Python multi-process and multi-thread basics
Analysis of Python object-oriented programming
CentOS 6.9 compile and install python
Python version of OpenCV installation
Python Data Science: Regularization Methods
CentOS 6 compile and install python 3