python data model (special method)

All special methods in python

 

  This part of the content can refer to the official website https://docs.python.org/3/reference/datamodel.html#special-method-names 

  There are a total of 83 special methods in python, of which 47 are used for arithmetic operations, bit operations and comparison operations. According to the arrangement in "Smooth Python", I excerpted the following two tables

  Table 1: Special methods not related to operators

category method name
String/byte sequence representation __repr__、__str__、__format__、__bytes__
Numeric conversion __abs__、__bool__、__complex__、__int__、__float__、__hash__、__index__
Collection simulation __len__、__getitem__、__setitem__、__delitem__、__contains__
iterate enumeration __iter__、__reversed__、__next__
callable mock __call__
context management __enter__、__exit__
instance creation and destruction __new__、__init__、__del__
property management __getattr__、__getattribute__、__setattr__、__delattr__、__dir__
property descriptor __get__、__set__、__delete__
Class related services __prepare__、__instancecheck__、__subclasscheck__

 

Table 2: Special methods associated with operators

category Method name and corresponding operator
unary operator __neg__ -、__pos__ +、__abs__ abs()
Numerous comparison operators __lt__ <、__le__ <=、__eq__ ==、__ne__ !=、__gt__ >、__ge__>=
arithmetic operators

__add__ +、__sub__ -、__mul__ *、__truediv__  /、__floordiv__ //、

__mod__  %、__divmod__ divmod()、__pow__ **或pow()、__round__ round()

reverse arithmetic operator __radd __ 、 __ rsub __ 、 __ rmul __ 、 __ rtruediv __ 、 __ rfloordiv __ 、 __ rmod __ 、 __ rdivmod __ 、 __ rpow__
Increment assignment arithmetic operator __iadd __ 、 __ isub __ 、 __ imul __ 、 __ itruediv __ 、 __ ifloordiv __ 、 __ imod __ 、 __ ipow__
bitwise operators __invert__ ~、__lshift__ <<、__rshift__ >>、__and__ &、__or__ |、__xor__ ^
reverse bitwise operator __rlshift__、__rrshift__、__rand__、__rxor__、__ror__
Increment assignment bitwise operator __ilshift__、__irshift__、__iand__、__ixor__、__ior__

 

How to use special methods:

 

1. The invocation of special methods is implicit, and usually your code does not need to use special methods directly. Unless there is a lot of metaprogramming, the frequency of calling special methods directly should be far less than the number of times you are going to implement them. The only exception might be the __init__ method, which may be used frequently in your code to call the superclass's constructor in your subclass's __init__ method.

2. Using special methods through built-in functions (such as len, iter, str, etc.) is the best choice. Not only do these built-in functions call special methods, they often provide additional benefits, but they are faster for built-in classes.

3. Don't arbitrarily add special methods, such as __foo__, etc., because although this name is not used internally by python now, it will not be necessary in the future.

 

- "Fluid Python"

## Original link: http://www.cnblogs.com/liao-sir/p/8416081.html



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325344612&siteId=291194637