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
8
  • 2
    Does the code you proposed above work? Commented Mar 14, 2016 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. Commented Mar 14, 2016 at 16:18
  • @Chris no, it's not working, just to show what I want Commented Mar 14, 2016 at 16:20
  • 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 Commented Mar 14, 2016 at 16:25
  • 1
    stackoverflow.com/questions/2705964/… Commented Mar 14, 2016 at 16:38

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.