本系列文章配套代码获取有以下三种途径:
-
可以在以下网站查看,该网站是使用JupyterLite搭建的web端Jupyter环境,因此无需在本地安装运行环境即可使用,首次运行浏览器需要下载一些配置文件(大约20M):
https://returu.github.io/Python_Basic/lab/index.html
-
也可以通过百度网盘获取,需要在本地配置代码运行环境,环境配置可以查看【Python基础】2.搭建Python开发环境:
链接:https://pan.baidu.com/s/1x2Ynh_VibPY2u-HWTaJo8w?pwd=mnsj提取码:mnsj
-
前往GitHub详情页面,单击 code 按钮,选择Download ZIP选项:
https://github.com/returu/Python_Basic
-----------------------------------------------------
1.魔术方法:
我们之前如需使用类中定义的函数,就需要主动调用才可以。
例如,下面类中我们定义一个equals()函数,通过判断学生年龄返回不同的值。
如需使用需要主动调用函数。
1# 定义类
2>>> class Student:
3...
4... def __init__(self , name , age):
5... self.name = name
6... self.age = age
7...
8... # 定义一个普通的equals()方法
9... def equals(self , value):
10... if self.age < value.age:
11... return f"{self.name}年龄小于{value.name}。"
12... if self.age > value.age:
13... return f"{self.name}年龄大于{value.name}。"
14... else:
15... return f"{self.name}和{value.name}同岁。"
16
17>>> student_1 = Student("Tom" , 12)
18>>> student_2 = Student("Lucy" , 15)
19>>> student_1.equals(student_2)
20'Tom年龄小于Lucy。'
下面使用魔术方法(将函数名进行调整,equals调整为__eq__):
1# 定义类
2>>> class Student:
3...
4... def __init__(self , name , age):
5... self.name = name
6... self.age = age
7...
8... # 魔术方法
9... def __eq__(self , value):
10... if self.age < value.age:
11... return f"{self.name}年龄小于{value.name}。"
12... if self.age > value.age:
13... return f"{self.name}年龄大于{value.name}。"
14... else:
15... return f"{self.name}和{value.name}同岁。"
16
17>>> student_1 = Student("Tom" , 12)
18>>> student_2 = Student("Lucy" , 15)
19>>> student_1 == student_2
20'Tom年龄小于Lucy。'
可以看到,魔术方法与普通方法的区别是不需要调用,需要触发。在类或对象的某些事件触发后会自动执行。
在Python中所有以双下划线起止的方法(格式为:"__方法名__"),都称为魔术方法(Magic Method)。之所以采用这种形式,是因为这种形式几乎不会被程序员选作自己的变量名。例如之前提到的__init__()方法就是一种魔法函数可以根据类定义以及传入的参数初始化新对象。
魔术方法其实是python内置方法,不需要主动调用,存在的目的是为了给python的解释器进行调用。
当我们对对象使用这些函数或者运算符时就会调用类中的对应魔法方法,如果希望根据自己的程序定制特殊功能的类,那么就需要对这些python的内置函数进行重写,以满足我们的需求。
回到上面的示例代码,我们只是将Python中内置的__eq__()函数进行了重写,赋予其新的功能。
2.Python中的魔术方法:
2.1 基本的魔术方法:
| 方法 |
说明 |
| __new__(cls[, ...]) | 创建对象的方法,当通过类去创建对象时就是调用__new__方法去创建的 |
| __init__(self[, ...]) | 初始化方法,当实例被创建的时候调用的初始化方法 |
| __del__(self) | 析构方法,当实例被销毁的时候调用的方法 |
| __call__(self[, args...]) | 允许一个类的实例像函数一样被调用:x(a, b) 调用 x.__call__(a, b) |
| __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) | 定义当描述符的值被删除时的行为 |
| 方法 |
说明 |
| __enter__(self) | 定义当使用 with 语句进入上下文管理器执行的方法 ,__enter__ 的返回值被 with 语句的目标或者 as 后的名字绑定 |
| __exit__(self, exc_type, exc_value, traceback) | 当with中的代码块执行完毕后调用的方法。一般被用来处理异常,清除工作或者做一些代码块执行完毕之后的日常工作 |
| 方法 |
说明 |
| __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.5 比较运算符:
| 方法 |
说明 |
| __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) | 定义当divmod()调用时的行为(a\b余数)(当左操作数不支持相应的操作是被调用) |
| __rpow__(self, other) | 定义当被power()调用或**运算时的行为(当左操作数不支持相应的操作是被调用) |
| __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) | 定义当被 round() 调用时的行为(需要返回恰当的值) |
| __index__(self) | 1. 当对象是被应用在切片表达式中时,实现整形强制转换 2. 如果你定义了一个可能在切片时用到的定制的数值型,你应该定义 __index__ 3. 如果 __index__ 被定义,则 int 也需要被定义,且返回相同的值 |
3.简单示例:
我们将内置的__add__(self, other)函数进行重写。
1>>> class Method:
2... def __init__(self , x , y):
3... self.x = x
4... self.y = y
5...
6... def __add__(self , other):
7... return (self.x + other.x , self.y + other.y)
8
9>>> a =Method(1,2)
10>>> b =Method(2,3)
11
12>>> a + b
13(3, 5)
本篇文章来源于微信公众号: 码农设计师
