Python Magic Methods & Operator Overloading All In One
__init__
&__add__
Magic Methods
__sub__
for -
__mul__
for *
__truediv__
for /
__floordiv__
for //
__mod__
for %
__pow__
for **
__and__
for &
__xor__
for ^
__or__
for |
The expression x + y
is translated into x.__add__(y)
.
However, if x hasn't implemented
add, and x and y are of different types
, then y.__radd__(x)
is called.
There are equivalent r
methods for all magic methods just mentioned.
__rsub__
for -
__rmul__
for *
__rtruediv__
for /
__rfloordiv__
for //
__rmod__
for %
__rpow__
for **
__rand__
for &
__rxor__
for ^
__ror__
for |
https://www.sololearn.com/learn/courses/python-intermediate/lesson/912154723?p=3
demos
#!/usr/bin/env python3
# coding: utf8
__author__ = 'xgqfrms'
__editor__ = 'vscode'
__version__ = '1.0.1'
__github__ = 'https://github.com/xgqfrms/Raspberry-Pi'
__git__ = 'https://github.com/xgqfrms/Raspberry-Pi.git'
__copyright__ = """
Copyright (c) 2012-2050, xgqfrms; mailto:[email protected]
"""
"""
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-01-01
* @updated 2023-07-01
*
* @description
* @augments
* @example
* @link
*
*/
"""
# print("Hello, Python 3
标签:__,Magic,Python,self,print,add,result,xgqfrms,Operator
From: https://www.cnblogs.com/xgqfrms/p/17565339.html