python高阶编程之魔法方法

python高阶编程之魔法方法,第1张

概述1. 魔法方法概念python中内置的有特殊功能的函数,以__开头,比如我们使用print函数去打印,实际上是调用了内置的__str__方法 2.__new__方法,创建对象时被调用通过代码来看下__new__和__init__被调用的顺序:1classMyClass:23def__init__(self):#初始

1.  魔法方法概念

  python中内置的有特殊功能的函数,以__开头,比如我们使用print函数去打印,实际上是调用了内置的__str__方法

 

2. __new__方法,创建对象时被调用

  通过代码来看下__new__和__init__被调用的顺序:

 1 class MyClass: 2  3     def __init__(self):  # 初始化属性 4         print("我是init,初始化对象方法") 5  6     def __new__(cls, *args, **kwargs): 7         print("我是new,创建对象方法") 8         return super().__new__(cls)  # 创建对象 9 10 11 MyClass()12 13 """14 运行结果:15 我是new,创建对象方法16 我是init,初始化对象方法17 """

  通过__new__ 方法,实现单例模式:

class Singleton:    __instance = None    def __init__(self, a):        self.a = a    def __new__(cls, *args, **kwargs):        if cls.__instance is None:  # 类没有实例,创建实例            cls.__instance = super().__new__(cls)        else:  # 类已有实例,更新属性            cls.__instance.__init__(*args, **kwargs)        return cls.__instanceprint(Singleton(1))print(Singleton(2))"""运行结果:<__main__.Singleton object at 0x000001B40283A160><__main__.Singleton object at 0x000001B40283A160>"""

3. 上下文管理器协议

  实现了__enter__、__exit__方法,我们使用 with open()打开文件,不需要手动关闭文件,接下来看代码案例:

 1 class Myfile: 2  3     def __init__(self, path): 4         self.path = path 5  6     def __enter__(self): 7         print("我是with开始,自动调用的方法:__enter__") 8         self.f = open(self.path, 'r', enCoding='utf-8') 9         return self10 11     def read(self):12         return self.f.read()13 14     def __exit__(self, exc_type, exc_val, exc_tb):15         self.f.close()16         print("我是with结束,自动调用的方法:__exit__")17 18 19 with Myfile(r"test.txt") as f:20     f.read()21     print("我是with中执行的内容")22 23 """24 执行结果:25 我是with开始,自动调用的方法:__enter__26 我是with中执行的内容27 我是with结束,自动调用的方法:__exit__28 """

4. __call__,对象可否被调用

  我们经常碰到的:TypeError: 'XXX' object is not callable,就是没有实现__call__

 1 class MyCall: 2     def __call__(self, *args, **kwargs): 3         print("我是对象被调用时,触发的方法") 4  5  6 MyCall()() 7 """ 8 运行结果: 9 我是对象被调用时,触发的方法10 """

 

5. __str__,使用print()函数调用的方法

  我们使用print(),控制台输出的内容是我们在__str__返回的内容

 1 class MyStr: 2  3     def __init__(self, desc: str): 4         self.desc = desc 5  6     def __str__(self): 7         return self.desc  # 返回值必须为str类型 8  9 10 print(MyStr("我是print时调用的方法"))11 """12 运行结果:13 我是print时调用的方法14 """

6. 算数运算符

  我们在使用对象的加、减、乘、除时,也是调用了对象内部的魔法方法

 1 class MyIntoperation: 2  3     def __init__(self, num: int): 4         self.num = num 5  6     def __add__(self, other): 7         self.num = self.num + other.num 8         return self 9 10     def __sub__(self, other):11         self.num = self.num - other.num12         return self13 14     def __str__(self):15         return str(self.num)16 17 18 a = MyIntoperation(1)19 b = MyIntoperation(2)20 print(f"相加结果:{a + b}")21 print(f"相减结果:{a - b}")22 """23 运行结果:24 相加结果:325 相减结果:126 """

7. 类属性访问机制

  我们在访问类属性:setattr()、self.属性、getattr()等 *** 作时,调用了对应的魔法方法

 1 class MyClass: 2  3     def __getattribute__(self, item): 4         print("我是访问类中存在的属性时,被调用的方法") 5         # 可以进行一系列的 *** 作,比如设置某些类属性不可以被访问 6         return super().__getattribute__(item) 7  8     def __getattr__(self, item): 9         print("我是访问类中不存在的属性时,被调用的方法")10         # 可以进行一系列的 *** 作11         raise AttributeError(f"{MyClass.__name__} 中没有属性:{item}")12 13     def __setattr__(self, key, value):14         print("我是给类设置属性时,被调用的方法")15         # 可以进行一系列的 *** 作16         return super().__setattr__(key, value)17 18     def __delattr__(self, item):19         print("我是删除类属性时,被调用的方法")20         # 可以进行一系列的 *** 作21         return super().__delattr__(item)22 23 24 my_class = MyClass()25 my_class.name = "我是类属性a"  # 设置类属性:name26 """27 运行结果:28 我是给类设置属性时,被调用的方法29 """30 print(my_class.name)  # 获取类属性:name31 """32 运行结果:33 我是访问类中存在的属性时,被调用的方法34 我是类属性a35 """36 del my_class.name  # 删除类属性:name37 """38 运行结果:39 我是删除类属性时,被调用的方法40 """41 print(my_class.name)  # 获取不存在的类属性:name42 """43 运行结果:44 我是访问类中不存在的属性时,被调用的方法45 AttributeError: MyClass 中没有属性:name46 """

 

 

 8. 魔法方法大全

魔法方法含义
                    基本的魔法方法
__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. 当对象是被应用在切片表达式中时,实现整形强制转换
2. 如果你定义了一个可能在切片时用到的定制的数值型,你应该定义 __index__
3. 如果 __index__ 被定义,则 __int__ 也需要被定义,且返回相同的值
上下文管理(with 语句)
__enter__(self)1. 定义当使用 with 语句时的初始化行为
2. __enter__ 的返回值被 with 语句的目标或者 as 后的名字绑定
__exit__(self, exc_type, exc_value, traceback)1. 定义当一个代码块被执行或者终止后上下文管理器应该做什么
2. 一般被用来处理异常,清除工作或者做一些代码块执行完毕之后的日常工作
                     容器类型
__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)时的行为
总结

以上是内存溢出为你收集整理的python高阶编程之魔法方法全部内容,希望文章能够帮你解决python高阶编程之魔法方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1185088.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存