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


Groups > comp.lang.python > #105469

Re: monkey patching __code__

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From "Sven R. Kunze" <srkunze@mail.de>
Newsgroups comp.lang.python
Subject Re: monkey patching __code__
Date Tue, 22 Mar 2016 13:01:27 +0100
Lines 35
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>
Mime-Version 1.0
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 7bit
X-Trace news.uni-berlin.de bZwyVSToehAdrKJEwBgKxQn2z0TxZWS/YU17eDhB4bEw==
Return-Path <srkunze@mail.de>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.018
X-Spam-Evidence '*H*': 0.96; '*S*': 0.00; 'defaults': 0.05; 'imported': 0.09; 'stable.': 0.09; 'django': 0.13; '2016': 0.16; 'mylist': 0.16; 'nightmare': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; '>>>': 0.20; 'explicit': 0.22; 'function,': 0.22; 'latter': 0.22; 'suppose': 0.22; 'code.': 0.23; 'seems': 0.23; 'thus': 0.24; 'header:In-Reply-To:1': 0.24; 'all.': 0.24; 'module': 0.25; 'not.': 0.27; 'updating': 0.27; 'see,': 0.27; 'function': 0.28; 'code': 0.30; 'problem': 0.33; 'received:10.0': 0.34; 'list': 0.34; 'best,': 0.35; 'could': 0.35; 'stable': 0.35; 'quite': 0.35; 'but': 0.36; 'modules': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'someone': 0.38; 'end': 0.39; 'why': 0.39; 'does': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'your': 0.60; 'skip:u 10': 0.61; 'charset:windows-1252': 0.62; 'more': 0.63; 'march': 0.64; 'injecting': 0.84; 'reminds': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/simple; d=mail.de; s=mail201212; t=1458648088; bh=qcB6mxhRSws13+Bc+nOz6VYYJ9QK0u4OdSoW39yn0KI=; h=Subject:To:References:From:Date:In-Reply-To:From; b=RpjelT/vPiDQmwMz+UEF2O19MY2Dhrm7QpjwpZagTzDFxwucTgtaYsbfYywcj0YNi mtumrS/ic0GD16NNvAUfk6fonzGQ1bZdVE8YVe3MJYaw5NefLoZbP/+ho4/ckpMTBe e9TeEOcFpb1qHKIxY0VPGgpohAqV50KZSOmFDqvQ=
In-Reply-To <CAG93HwFr-yFqjex_U4yZs5WUuLCCbCJDJfso2Kp6G0F3BdKOZQ@mail.gmail.com>
X-purgate clean
X-purgate This mail is considered clean (visit http://www.eleven.de for further information)
X-purgate-type clean
X-purgate-Ad Categorized by eleven eXpurgate (R) http://www.eleven.de
X-purgate This mail is considered clean (visit http://www.eleven.de for further information)
X-purgate clean
X-purgate-size 1163
X-purgate-ID 154282::1458648088-000009F3-63F60F74/0/0
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.21
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Xref csiph.com comp.lang.python:105469

Show key headers only | 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