[Python进阶] 定制类:模拟篇

4.10.5 模拟篇

4.10.5.1 call

通过__call__魔法方法可以像使用函数一样使用对象。通过括号的方式调用,也可以像函数一样传入参数:

py 复制代码
from icecream import ic


class Multiplier:
    def __init__(self, mul):
        self.mul = mul

    def __call__(self, arg):
        return self.mul * arg


o = Multiplier(3)
ic(o(4))

11:15:45|> o(4): 12

4.10.5.2 len

当我们对某个自定义对象使用len函数时,其实就是在调用这个魔术方法。

py 复制代码
from icecream import ic


class MyList:
    def __init__(self, data):
        self._data = data

    def __len__(self):
        return len(self._data)


x = MyList([1, 2, 3])
ic(len(x))
if x:
    ic('OK')

11:23:21|> len(x): 3

11:23:21|> 'OK'

当我们用自定义的对象作为判断条件时,如果我们的自定义对象中没有定义__bool__魔术方法,那么会通过__len__魔术方法进行判断,不为空则返回True。

4.10.5.3 getitem 、setitem

当我们尝试用[]的形式调用或赋值对象中的元素时会用调用这两个魔术方法:

py 复制代码
from icecream import ic


class MyList:
    def __init__(self, data):
        self._data = data

    def __getitem__(self, key):
        return self._data[key]

    def __setitem__(self, key, value):
        self._data[key] = value


x = MyList([1, 2, 3])
ic(x[2])
x[2] = 5
ic(x[2])

14:48:12|> x2: 3

14:48:13|> x2: 5

4.10.5.4 delitem

在del objn时会调用这个魔术方法。

py 复制代码
from icecream import ic


class MyList:
    def __init__(self, data):
        self._data = data

    def __getitem__(self, key):
        return self._data[key]

    def __delitem__(self, key):
        self._data = self._data[0:key] + self._data[key + 1:]


x = MyList([1, 2, 3])
ic(x[1])
del x[1]
ic(x[1])

14:55:20|> x1: 2

14:55:20|> x1: 3

4.10.5.5 reversed

reversed :reverse(obj)

当用Python内置的函数reverse对象时会调用对象内的该方法。

py 复制代码
from icecream import ic


class MyList:
    def __init__(self, data):
        self._data = data

    def __getitem__(self, key):
        return self._data[key]

    def __reversed__(self):
        return MyList(self._data[::-1])


x = MyList([1, 2, 3])
ic(reversed(x)._data)

15:03:55|> reversed(x)._data: 3, 2, 1

4.10.5.6 contains

contains :item in obj

做in操作时会调用该方法。

py 复制代码
from icecream import ic


class MyList:
    def __init__(self, data):
        self._data = data

    def __contains__(self, item):
        return item in self._data


x = MyList([1, 2, 3])
ic(1 in x)
ic(4 in x)

15:05:22|> 1 in x: True

15:05:22|> 4 in x: False

4.10.5.7 iter

iter :iter(obj)

返回对象的迭代器(iter)时会调用该方法。

4.10.5.8 missing

这个魔术方法必须是Python的字典类型数据的子类中才有作用。当在字典中找一个key而找不到时,会调用这个方法。

py 复制代码
from icecream import ic


class MyDict(dict):
    def __missing__(self, key):
        return 1


d = MyDict()
ic(d[0])

15:10:06|> d0: 1

4.10.5.9 enter 、exit

这两个魔术方法和上下文管理器有关,在之前的章节中已经详细介绍了。这里就不赘述了。

相关推荐
总要学点什么19 小时前
第89篇:三维特效-螺旋圆效果——让螺旋臂卷起一圈圈外扩的涟漪
开发语言·javascript·ecmascript·webgl·cesium·空间分析·1024程序员节
前端精髓20 小时前
结构化输出with_structured_output(抽卡)
python·langchain
尹人入圣20 小时前
目前热门的IP驱动产业新场景新工具
网络·python·网络协议·tcp/ip
开开心心_Every21 小时前
快速隐藏程序工具自定义快捷键一键切换
java·开发语言·前端·spring·智能手机·word·intellij-idea
chenbingjie_c21 小时前
C++ 多态完全指南:从虚函数重写到虚函数表原理
开发语言·c++
Nuanyt21 小时前
Agent 学习笔记(下篇三):上线工程、进阶选择与操作界面(Stage 7 / 7.5 / 8 + 延伸路线)
笔记·python·学习·memory·aiagent·harness
吴卫斌21 小时前
定投ETF实战指南(四):行业/主题ETF——定投的“进攻端“
开发语言·python·股票·量化
2601_967987371 天前
Python——入门级(定义功能def函数)
python·函数·可变参数·默认参数·关键字参数
超级架构师1 天前
PRAXO-CORE 2.5.0:用 conformance 和显式边界构建 Go Agent 框架
开发语言·人工智能·后端·golang·ai编程·智能体框架·praxo-core
叼烟扛炮1 天前
Linux 学习笔记(四十三):网络编程进阶——自定义协议解决 TCP 粘包与守护进程
linux·开发语言·网络·笔记·tcp/ip