如何修改 Python 的内置模块



我想在python的内置模块中添加函数。例如,list中没有find函数。我想加上它。我还想增加一些其他的功能,谁能告诉我怎么做?我有办法做到吗?

向builtins模块添加函数很简单:

import builtins
builtins.find = my_find_function

但是内置类通常是不可变的,所以它们不能被改变。

>>> list.hello = "hello"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't set attributes of built-in/extension type 'list'

最新更新