python - 覆盖已安装包方法的最佳方法

标签 python

我正在使用django-compress进行压缩。根据需要,我想更改 ..../dist-packages/compress/utils.py 中的功能。我如何覆盖这个函数。

最佳答案

import compress.utils
utils.compress.function_to_override = overriding_function

Monkey patch它。您需要在将 utils 导入到其他地方之前执行此操作。

或者,您可以创建一个新的 Python 模块,如下所示:

# fakeutils.py
from compress.utils import *

# make the following line match exactly the overridden function
def function_to_override(var1, etc1, etc2):
    # your version of the function

然后导入该模块:

import fakeutils as utils

关于python - 覆盖已安装包方法的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7074829/

相关文章:

python - PyQt:如何使用 QAxWidget 通过引用发送参数

python - DRF : how to not allow create() in Serializer

python - 将多索引中的日期时间设置为该月的最后一天

python - 如何输出网络的第二层?

Python Pandas 删除重复的单元格 - 保留行

python - 在不知道网页结构的情况下使用 Scrapy 抓取所有文本

python - 在 OpenCV Python 中裁剪矩形

python - 查找自上次使用 python imaplib2 检查后添加到 imap 邮箱的新邮件?

python - 迭代器协议(protocol)。是黑魔法吗?

python - 如何从 OpenCV 中已标记的图像中获取区域属性?