How to extend/patch an existing module or package?
I want to extend some locale-specific features of a python application named OpenERP. All I need is implementing a third party module.function that would be called every tim开发者_JAVA百科e OpenERP calls locale.setlocale() function without changing neither OpenERP nor locale module source code.
The only way I can imagine is provide a module named locale.py inside main application package dir, but It seems that is an unpythonic workaround.
Look up Monkey Patching. It's not most elegant technique, but sometimes it's the only option.
In your case you can substitute your own function for locale.setlocale()
which will do whatever you want. It would look something like that:
import locale
original_setlocale = locale.setlocale
def my_setlocale(category, locale=None):
# Do anything you want
# optionally call original function
original_setlocale(category, locale)
精彩评论