如何修改 python 的内置模块
how to modify python's built in modules
我想向 python 的内置模块添加功能。例如,列表中没有查找功能。我想添加它。我还想添加一些其他功能。有人可以告诉我我该怎么做吗?他们有什么办法可以做到吗?
向内置模块添加函数很简单:
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'
我想向 python 的内置模块添加功能。例如,列表中没有查找功能。我想添加它。我还想添加一些其他功能。有人可以告诉我我该怎么做吗?他们有什么办法可以做到吗?
向内置模块添加函数很简单:
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'