
On Fri, Aug 7, 2020 at 6:24 PM Kazantcev Andrey heckad@yandex.ru wrote:
The problem in this code
lib.py
from json import dumps def some_func(): # do something res = dumps(...) # do something
If I patch dumps like you propose lib doesn't see any change. Also, it's all hacks. I wish dump and load themselves could take parameters from somewhere else, and that was the standard behaviour.
PLEASE include context when you reply.
Yes, I'm aware that my original demo wouldn't work with this kind of thing. But (1) does that actually happen? and (2) my second example will work, as long as you do it before this library imports anything from json.
Why do you want dump and load to take parameters from "somewhere else"? As a general rule, it's better for them to get their parameters entirely from, well, their parameters. What you're trying to do is override someone else's code, and that basically means monkeypatching the library, or monkeypatching the json module. Take your pick.
ChrisA