[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 getitemsetitem

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

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|> x[2]: 3

14:48:13|> x[2]: 5

4.10.5.4 delitem

在del obj[n]时会调用这个魔术方法。

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|> x[1]: 2

14:55:20|> x[1]: 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|> d[0]: 1

4.10.5.9 enterexit

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

相关推荐
胖达不服输1 小时前
「日拱一码」020 机器学习——数据处理
人工智能·python·机器学习·数据处理
吴佳浩2 小时前
Python入门指南-番外-LLM-Fingerprint(大语言模型指纹):从技术视角看AI开源生态的边界与挑战
python·llm·mcp
吴佳浩2 小时前
Python入门指南-AI模型相似性检测方法:技术原理与实现
人工智能·python·llm
liulilittle2 小时前
LinkedList 链表数据结构实现 (OPENPPP2)
开发语言·数据结构·c++·链表
叶 落2 小时前
计算阶梯电费
python·python 基础·python 入门
2401_891957313 小时前
list的一些特性(C++)
开发语言·c++
二十雨辰3 小时前
[尚庭公寓]07-Knife快速入门
java·开发语言·spring
Python大数据分析@3 小时前
Origin、MATLAB、Python 用于科研作图,哪个最好?
开发语言·python·matlab
编程零零七3 小时前
Python巩固训练——第一天练习题
开发语言·python·python基础·python学习·python练习题
我爱Jack4 小时前
时间与空间复杂度详解:算法效率的度量衡
java·开发语言·算法