Overriding static methods

381 Views Asked by At

I have to override a function on this egg dicttoxml however the problem is that not being a class I don't know how to override just a particular function.

I tried by creating a new "package" and importing * and then writing out the function I wanted to override but it's ignoring it all together, I'm not sure how or if it can be done.

Edit: This a gist to the code I changed the function is the same just changed item with segment

Edit2: I also added the way I import from the the other file, I called the new "package" dicttoxml_fast

1

There are 1 best solutions below

2
On

this :

`from libs.dicttoxml_fast.dicttoxml_fast import dicttoxml` 

cannot work with your current dicttoxml_fast module. You can import this way instead:

`from libs.dicttoxml_fast import dicttoxml_fast as dicttoxml`

but this will not make other functions from the original dicttoxml use your own version of convert_list. If that was your plan, you'll have to either fork the original (FWIW the author might be interested in your own implementation if it's strictly equivalent and faster) or monkeypatch it:

# 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

then use from libs.dicttoxml_fast.dicttoxml_fast import dicttoxml in your client code.