在Python3.x中,是否可以在一个项被附加到列表之前运行一个函数?你知道吗
我有一个从列表继承的类,带有一些附加的自定义函数。我想对添加到此列表中的任何元素的数据执行一系列检查。如果添加的元素不符合某些条件,列表将引发错误。你知道吗
class ListWithExtraFunctions(list):
def __beforeappend__(self):
... run some code ...
... perform checks ...
... raise error if checks fail ...
Tags:
此选项与Vaultah编写的解决方案非常相似。它只使用“try…except”,允许您以某种方式处理异常。你知道吗
如果
value
通过所有检查,则定义ListWithExtraFunctions.append
并调用super().append(value)
:相关问题 更多 >
编程相关推荐