0

For example Python has standard module json. It is not a class.

json.dumps defined as function with list of default args. I want redefine one of default args - cls for this function. How I should do it?

json._dumps = json.dumps

def dumps(*kargs, **kwargs):
    kwarsg['cls'] = MyClass
    return json._dumps(*kargs, **kwargs)

json.dumps = dumps
Raf
  • 622
  • 9
  • 19
  • 2
    Does the code you proposed above work? – ChrisGPT was on strike Mar 14 '16 at 16:18
  • 2
    Rather than patching json, you may create your own dumps function like you do here and import this to your modules instead of json.dumps. – Jérôme Mar 14 '16 at 16:18
  • @Chris no, it's not working, just to show what I want – Raf Mar 14 '16 at 16:20
  • @Jérôme Yes, I can, but I find way to set default args for existing function, or it impossible? – Raf Mar 14 '16 at 16:22
  • 3
    I would highly recommend you to avoid monkeypatching of the stdlib, as it can lead to unforseen consequences. Imagine some other library uses `json.dumps`, it expects the "vanilla" output, but this way it will get a modified version, which may lead to some errors. As said above - define your own dump function – yedpodtrzitko Mar 14 '16 at 16:25
  • @Raf, in what scope? A single file? A module? And for what purpose? I suspect this is an [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Make sure you're asking about your _real goal_, not what you think the solution might be. – ChrisGPT was on strike Mar 14 '16 at 16:25
  • I don't want do monkeypatching, I am looking for way to do inheritance like with classes. If this no way to do it, I write my dumps function. – Raf Mar 14 '16 at 16:30
  • 1
    http://stackoverflow.com/questions/2705964/how-do-i-extend-a-python-module-python-twitter – Karoly Horvath Mar 14 '16 at 16:38

0 Answers0