Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #105469

Re: monkey patching __code__

From "Sven R. Kunze" <srkunze@mail.de>
Newsgroups comp.lang.python
Subject Re: monkey patching __code__
Date 2016-03-22 13:01 +0100
Message-ID <mailman.5.1458648096.2244.python-list@python.org> (permalink)
References <56EBEB5A.7050207@mail.de> <CAG93HwGKrN8y3k=3OFtHq-PbMjFzy=1U99-ZuJOLdEq69gosfg@mail.gmail.com> <56EED3DE.7050101@mail.de> <CAG93HwFr-yFqjex_U4yZs5WUuLCCbCJDJfso2Kp6G0F3BdKOZQ@mail.gmail.com>

Show all headers | View raw


On 21.03.2016 21:42, Matt Wheeler wrote:
> On 20 March 2016 at 16:46, Sven R. Kunze <srkunze@mail.de> wrote:
>> On 19.03.2016 00:58, Matt Wheeler wrote:
>>> I know you have a working solution now with updating the code &
>>> defaults of the function, but what about just injecting your function
>>> into the modules that had already imported it after the
>>> monkeypatching?
>>>
>>> Seems perhaps cleaner, unless you'd end up having to do it to lots of
>>> modules...
>> Why do you consider it cleaner?
> I think it would be more explicit and understandable for someone
> reading your code.
>
> I suppose it's quite subjective :)

As far as I can see, the code replacement approach solves the problem 
once and for all. Thus is far more stable.

Manually finding out every single module that might or might not have 
imported "reverse" before we could monkeypatch it might result in a 
maintenance nightmare (just think about a Django upgrade).


It reminds me of list replacement:

mylist = newlist
mylist[:] = newlist

The latter keeps the reference stable whereas the former does not. Same 
with monkeypatching.


Best,
Sven

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: monkey patching __code__ "Sven R. Kunze" <srkunze@mail.de> - 2016-03-22 13:01 +0100

csiph-web