覆盖静态方法

时间:2014-12-10 11:31:06

标签: python egg

我必须覆盖这个蛋dicttoxml上的函数,但问题是不是一个类我不知道如何覆盖一个特定的函数。

我尝试创建一个新的“包”并导入*然后写出我想要覆盖的函数,但是它一起忽略它,我不知道如何或是否可以完成。

编辑: 这个代码的gist我改变了函数是与段

相同的刚改变的项目

Edit2:我还添加了从其他文件导入的方式,我调用了新的“package”dicttoxml_fast

1 个答案:

答案 0 :(得分:0)

这个:

`from libs.dicttoxml_fast.dicttoxml_fast import dicttoxml` 

无法使用您当前的dicttoxml_fast模块。你可以这样导入:

`from libs.dicttoxml_fast import dicttoxml_fast as dicttoxml`

但这不会使原始dicttoxml中的其他功能使用您自己的convert_list版本。如果这是你的计划,你将不得不分割原文(作者可能对你自己的实现感兴趣,如果它是严格等同和更快)或者monkeypatch它:

# dicttoxml_fast.py

import dicttoxml
from dicttoxml import *

def convert_list(items, ids, parent, attr_type):
    # your code here

# apply the monkeypatch
dicttoxml.convert_list = convert_list

然后在您的客户端代码中使用from libs.dicttoxml_fast.dicttoxml_fast import dicttoxml