python魔法方法大全
1、python魔法方法详解:
python魔法方法是可以修改重载的,如果你的对象实现(重载)了这些方法中的某一个,那么这个方法就会在特殊的情况下被 Python 所调用,你可以定义自己想要的行为,而这一切都是自动发生的。Python 的魔术方法非常强大,了解正确的方法去使用非常重要!
以下为python里魔法方法大全总结:
|
魔法方法 |
含义 |
|
基本的魔法方法 |
|
|
__new__(cls[, ...]) |
1. __new__ 是在一个对象实例化的时候所调用的第一个方法 |
|
__init__(self[, ...]) |
构造器,当一个实例被创建的时候调用的初始化方法 |
|
__del__(self) |
析构器,当一个实例被销毁的时候调用的方法 |
|
__call__(self[, args...]) |
允许一个类的实例像函数一样被调用:x(a, b) 调用 x.__call__(a, b) |
|
__len__(self) |
定义当被 len() 调用时的行为 |
|
__repr__(self) |
定义当被 repr() 调用时的行为 |
|
__str__(self) |
定义当被 str() 调用时的行为 |
|
__bytes__(self) |
定义当被 bytes() 调用时的行为 |
|
__hash__(self) |
定义当被 hash() 调用时的行为 |
|
__bool__(self) |
定义当被 bool() 调用时的行为,应该返回 True 或 False |
|
__format__(self, format_spec) |
定义当被 format() 调用时的行为 |
|
有关属性 |
|
|
__getattr__(self, name) |
定义当用户试图获取一个不存在的属性时的行为 |
|
__getattribute__(self, name) |
定义当该类的属性被访问时的行为 |
|
__setattr__(self, name, value) |
定义当一个属性被设置时的行为 |
|
__delattr__(self, name) |
定义当一个属性被删除时的行为 |
|
__dir__(self) |
定义当 dir() 被调用时的行为 |
|
__get__(self, instance, owner) |
定义当描述符的值被取得时的行为 |
|
__set__(self, instance, value) |
定义当描述符的值被改变时的行为 |
|
__delete__(self, instance) |
定义当描述符的值被删除时的行为 |
|
比较操作符 |
|
|
__lt__(self, other) |
定义小于号的行为:x < y 调用 x.__lt__(y) |
|
__le__(self, other) |
定义小于等于号的行为:x <= y 调用 x.__le__(y) |
|
__eq__(self, other) |
定义等于号的行为:x == y 调用 x.__eq__(y) |
|
__ne__(self, other) |
定义不等号的行为:x != y 调用 x.__ne__(y) |
|
__gt__(self, other) |
定义大于号的行为:x > y 调用 x.__gt__(y) |
|
__ge__(self, other) |
定义大于等于号的行为:x >= y 调用 x.__ge__(y) |
|
算数运算符 |
|
|
__add__(self, other) |
定义加法的行为:+ |
|
__sub__(self, other) |
定义减法的行为:- |
|
__mul__(self, other) |
定义乘法的行为:* |
|
__truediv__(self, other) |
定义真除法的行为:/ |
|
__floordiv__(self, other) |
定义整数除法的行为:// |
|
__mod__(self, other) |
定义取模算法的行为:% |
|
__divmod__(self, other) |
定义当被 divmod() 调用时的行为 |
|
__pow__(self, other[, modulo]) |
定义当被 power() 调用或 ** 运算时的行为 |
|
__lshift__(self, other) |
定义按位左移位的行为:<< |
|
__rshift__(self, other) |
定义按位右移位的行为:>> |
|
__and__(self, other) |
定义按位与操作的行为:& |
|
__xor__(self, other) |
定义按位异或操作的行为:^ |
|
__or__(self, other) |
定义按位或操作的行为:| |
|
反运算 |
|
|
__radd__(self, other) |
(与上方相同,当左操作数不支持相应的操作时被调用) |
|
__rsub__(self, other) |
(与上方相同,当左操作数不支持相应的操作时被调用) |
|
__rmul__(self, other) |
(与上方相同,当左操作数不支持相应的操作时被调用) |
|
__rtruediv__(self, other) |
(与上方相同,当左操作数不支持相应的操作时被调用) |
|
__rfloordiv__(self, other) |
(与上方相同,当左操作数不支持相应的操作时被调用) |
|
__rmod__(self, other) |
(与上方相同,当左操作数不支持相应的操作时被调用) |
|
__rdivmod__(self, other) |
(与上方相同,当左操作数不支持相应的操作时被调用) |
|
__rpow__(self, other) |
(与上方相同,当左操作数不支持相应的操作时被调用) |
|
__rlshift__(self, other) |
(与上方相同,当左操作数不支持相应的操作时被调用) |
|
__rrshift__(self, other) |
(与上方相同,当左操作数不支持相应的操作时被调用) |
|
__rand__(self, other) |
(与上方相同,当左操作数不支持相应的操作时被调用) |
|
__rxor__(self, other) |
(与上方相同,当左操作数不支持相应的操作时被调用) |
|
__ror__(self, other) |
(与上方相同,当左操作数不支持相应的操作时被调用) |
|
增量赋值运算 |
|
|
__iadd__(self, other) |
定义赋值加法的行为:+= |
|
__isub__(self, other) |
定义赋值减法的行为:-= |
|
__imul__(self, other) |
定义赋值乘法的行为:*= |
|
__itruediv__(self, other) |
定义赋值真除法的行为:/= |
|
__ifloordiv__(self, other) |
定义赋值整数除法的行为://= |
|
__imod__(self, other) |
定义赋值取模算法的行为:%= |
|
__ipow__(self, other[, modulo]) |
定义赋值幂运算的行为:**= |
|
__ilshift__(self, other) |
定义赋值按位左移位的行为:<<= |
|
__irshift__(self, other) |
定义赋值按位右移位的行为:>>= |
|
__iand__(self, other) |
定义赋值按位与操作的行为:&= |
|
__ixor__(self, other) |
定义赋值按位异或操作的行为:^= |
|
__ior__(self, other) |
定义赋值按位或操作的行为:|= |
|
一元操作符 |
|
|
__pos__(self) |
定义正号的行为:+x |
|
__neg__(self) |
定义负号的行为:-x |
|
__abs__(self) |
定义当被 abs() 调用时的行为 |
|
__invert__(self) |
定义按位求反的行为:~x |
|
类型转换 |
|
|
__complex__(self) |
定义当被 complex() 调用时的行为(需要返回恰当的值) |
|
__int__(self) |
定义当被 int() 调用时的行为(需要返回恰当的值) |
|
__float__(self) |
定义当被 float() 调用时的行为(需要返回恰当的值) |
|
__round__(self[, n]) |
定义当被 round() 调用时的行为(需要返回恰当的值) |
|
__index__(self) |
1. 当对象是被应用在切片表达式中时,实现整形强制转换 |
|
上下文管理(with 语句) |
|
|
__enter__(self) |
1. 定义当使用 with 语句时的初始化行为 |
|
__exit__(self, exc_type, exc_value, traceback) |
1. 定义当一个代码块被执行或者终止后上下文管理器应该做什么 |
|
容器类型 |
|
|
__len__(self) |
定义当被 len() 调用时的行为(返回容器中元素的个数) |
|
__getitem__(self, key) |
定义获取容器中指定元素的行为,相当于 self[key] |
|
__setitem__(self, key, value) |
定义设置容器中指定元素的行为,相当于 self[key] = value |
|
__delitem__(self, key) |
定义删除容器中指定元素的行为,相当于 del self[key] |
|
__iter__(self) |
定义当迭代容器中的元素的行为 |
|
__reversed__(self) |
定义当被 reversed() 调用时的行为 |
|
__contains__(self, item) |
定义当使用成员测试运算符(in 或 not in)时的行为 |
2、Python 什么时候会调用到反运算的魔法方法?
答:例如 a + b,如果 a 对象的
__add__ 方法没有实现或者不支持相应的操作,那么 Python 就会自动调用 b 的 __radd__ 方法
举例如下:

3、请问如何在继承的类中调用基类的方法?
答:使用 super() 这个 BIF 函数。例如:
- class Derived(Base):
- def
meth (self): - super(Derived, self).meth()
---恢复内容结束---
python魔法方法大全的更多相关文章
- 【转】Python 魔法方法大全
转载自鱼C论坛:http://bbs.fishc.org/thread-48793-1-2.html 据说,Python 的对象天生拥有一些神奇的方法,它们总被双下划线所包围,他们是面向对象的 Pyt ...
- Python魔法方法总结及注意事项
1.何为魔法方法: Python中,一定要区分开函数和方法的含义: 1.函数:类外部定义的,跟类没有直接关系的:形式: def func(*argv): 2.方法:class内部定义的函数(对象的方法 ...
- python魔法方法:__getattr__,__setattr__,__getattribute__
python魔法方法:__getattr__,__setattr__,__getattribute__ 难得有时间看看书....静下心来好好的看了看Python..其实他真的没有自己最开始想的那么简单 ...
- python 魔法方法补充(__setattr__,__getattr__,__getattribute__)
python 魔法方法补充 1 getattribute (print(ob.name) -- obj.func())当访问对象的属性或者是方法的时候触发 class F(object): def _ ...
- 1. Python 魔法方法
Python 魔法方法 基础: 如果你想... 所以,你写... Python调用... 初始化一个实例 x = MyClass() x.__init__() 作为一个字符串的"官方&quo ...
- with上下文管理 python魔法方法
with语法在Python里很常见, 主要的利好是使用代码更简洁. 常见的使用场景有: 1. 资源对象的获取与释放. 使用with可以简化try...finally ... 2. 在不修改函数代码的前 ...
- python 魔法方法诠释
什么是Python魔法方法 什么是魔法方法呢?它们在面向对象的Python的处处皆是.它们是一些可以让你对类添加"魔法"的特殊方法. 它们经常是两个下划线包围来命名的(比如 ini ...
- python 魔法方法
I am not a creator, I just a porter. Note: Everything is object in python. 对于Python来说一切都是对象,也就是函数的参数 ...
- python 魔法方法之:__getitem__ __setitem__ __delitem__
h2 { color: #fff; background-color: #7CCD7C; padding: 3px; margin: 10px 0px } h3 { color: #fff; back ...
随机推荐
- 寒假短期学习计划 - C++
寒假短期学习计划 - C++ 一.所选课程 && 相关 0.选以下课的理由: 选课理由0: 只是短期的计划,先选些短视频感受:之后再视情况选其他课: 选课理由1: 难度低,以前自学过一 ...
- php解决约瑟夫环的问题
php里面解决约瑟夫环还是比较方面的,但是下面的方法太费空间 <?php class SelectKing{ private $m;//幅度 private $n;//总数 public fun ...
- HBase性能优化 Java Api
1. 使用“连接池” 如果每次和Hbase交互时都去新建连接的话,显然是低效率的,HBase也提供类连接池相关的API. 1.1. HTablePool 早期的API中使用它,但很不幸,现在它已经过时 ...
- VM下,装centos7系统,配置nginx的问题
一.流程 1.先安装nginx依赖的包 (1)yum install gcc-c++ (2)yum install -y pcre pcre-devel (3)yum install -y zlib ...
- Golang Http Server源码阅读
建议看这篇文章前先看一下net/http文档 http://golang.org/pkg/net/http/ net.http包里面有很多文件,都是和http协议相关的,比如设置cookie,head ...
- BZOJ5418:[NOI2018]屠龙勇士(exCRT,exgcd,set)
Description Input Output Sample Input 23 33 5 74 6 107 3 91 9 10003 23 5 64 8 71 1 11 1 Sample Outpu ...
- 【转】Android系统中的.apk文件和dex文件
1. *.apk文件 APK是Android Package的缩写,即Android安装包.通过将APK文件直接传到Android模拟器或Android手机中执行即可安装. 使用Android打包工具 ...
- 《信息安全技术》实验一 PGP的原理与使用
<信息安全技术>实验一 PGP的原理与使用(macOS High Sierra下实现) 实验目的 理解传统加密.公钥加密.混合加密.数字签名等概念 理解公钥.私钥.会话密钥.对称密钥等概念 ...
- ethers.js-4-Contracts
Contracts A Contract is an abstraction of an executable program on the Ethereum Blockchain. A Contra ...
- PAT乙级1021
1021 个位数统计 (15 分) 给定一个 k 位整数 N=dk−110k−1+⋯+d1101+d0 (0≤di≤9, i=0,⋯,k−1, dk−1& ...