Python operator - Standard operators as functions

Python operator - Standard operators as functions

  • 1. Mapping Operators to Functions
  • 2. In-place Operators
  • References

https://docs.python.org/3/library/operator.html

The operator module exports a set of efficient functions corresponding to the intrinsic operators of Python. For example, operator.add(x, y) is equivalent to the expression x + y. Many function names are those used for special methods, without the double underscores. For backward compatibility, many of these have a variant with the double underscores kept. The variants without the double underscores are preferred for clarity.
operator 模块提供了一套与 Python 的内置运算符对应的高效率函数。例如,operator.add(x, y) 与表达式 x+y 相同。许多函数名与特殊方法名相同,只是没有双下划线。为了向后兼容性,也保留了许多包含双下划线的函数。为了表述清楚,建议使用没有双下划线的函数。

  • operator.matmul(a, b)
    operator.__matmul__(a, b)

Return a @ b.

#!/usr/bin/env python
# coding=utf-8

import numpy as np

A = np.array([[1, 2],
              [3, 4],
              [5, 6]])
B = np.array([[2, 4],
              [1, 3]])

C = A @ B
print(f"{C}")

/home/yongqiang/miniconda3/bin/python /home/yongqiang/stable_diffusion_work/stable_diffusion_diffusers/yongqiang.py 
[[ 4 10]
 [10 24]
 [16 38]]
 
Process finished with exit code 0

1. Mapping Operators to Functions

This table shows how abstract operations correspond to operator symbols in the Python syntax and the functions in the operator module.
以下表格显示了抽象运算是如何对应于 Python 语法中的运算符和 operator 模块中的函数的。

Operation Syntax Function
Addition a + b add(a, b)
Concatenation (字符串拼接) seq1 + seq2 concat(seq1, seq2)
Containment Test (包含测试) obj in seq contains(seq, obj)
Division a / b truediv(a, b)
Division a // b floordiv(a, b)
Bitwise And (按位与) a & b and_(a, b)
Bitwise Exclusive Or (按位异或) a ^ b xor(a, b)
Bitwise Inversion (按位取反) ~ a invert(a)
Bitwise Or (按位或) a | b or_(a, b)
Exponentiation (取幂) a ** b pow(a, b)
Identity a is b is_(a, b)
Identity a is not b is_not(a, b)
Indexed Assignment obj[k] = v setitem(obj, k, v)
Indexed Deletion del obj[k] delitem(obj, k)
Indexing obj[k] getitem(obj, k)
Left Shift a << b lshift(a, b)
Modulo (取模) a % b mod(a, b)
Multiplication a * b mul(a, b)
Matrix Multiplication a @ b matmul(a, b)
Negation (Arithmetic) - a neg(a)
Negation (Logical) not a not_(a)
Positive + a pos(a)
Right Shift a >> b rshift(a, b)
Slice Assignment seq[i:j] = values setitem(seq, slice(i, j), values)
Slice Deletion del seq[i:j] delitem(seq, slice(i, j))
Slicing seq[i:j] getitem(seq, slice(i, j))
String Formatting s % obj mod(s, obj)
Subtraction a - b sub(a, b)
Truth Test obj truth(obj)
Ordering (比较) a < b lt(a, b)
Ordering a <= b le(a, b)
Equality a == b eq(a, b)
Difference a != b ne(a, b)
Ordering a >= b ge(a, b)
Ordering a > b gt(a, b)

2. In-place Operators

Many operations have an “in-place” version. Listed below are functions providing a more primitive access to in-place operators than the usual syntax does; for example, the statement x += y is equivalent to x = operator.iadd(x, y).

  • operator.iadd(a, b)
    operator.__iadd__(a, b)

a = iadd(a, b) is equivalent to a += b.

  • operator.imul(a, b)
    operator.__imul__(a, b)

a = imul(a, b) is equivalent to a *= b.

  • operator.imatmul(a, b)
    operator.__imatmul__(a, b)

a = imatmul(a, b) is equivalent to a @= b.

References

[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/

你可能感兴趣的:(Python,3,-,Python,2,-,Cython,Python,operator,operators,functions)