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


Groups > comp.lang.python > #105221 > unrolled thread

Re: monkey patching __code__

Started by"Sven R. Kunze" <srkunze@mail.de>
First post2016-03-18 15:42 +0100
Last post2016-03-18 15:42 +0100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: monkey patching __code__ "Sven R. Kunze" <srkunze@mail.de> - 2016-03-18 15:42 +0100

#105221 — Re: monkey patching __code__

From"Sven R. Kunze" <srkunze@mail.de>
Date2016-03-18 15:42 +0100
SubjectRe: monkey patching __code__
Message-ID<mailman.324.1458312124.12893.python-list@python.org>
On 18.03.2016 15:33, Sven R. Kunze wrote:
> On 18.03.2016 15:23, Ian Kelly wrote:
>> On Fri, Mar 18, 2016 at 7:47 AM, Ian Kelly <ian.g.kelly@gmail.com> 
>> wrote:
>>> Your patched version takes two extra arguments. Did you add the
>>> defaults for those to the function's __defaults__ attribute?
>> And as an afterthought, you'll likely need to replace the function's
>> __globals__ with your own as well.


def f(a, b=None, c=None):
     print(a, b, c)

def f_patch(a, b=None, c=None, d=None, e=None):
     print(a, b, c, d, e)


f.__code__ = f_patch.__code__
f.__defaults__ = f_patch.__defaults__
f.__kwdefaults__ = f_patch.__kwdefaults__
f.__globals__ = f_patch.__globals__   <<<<< crashes here with 
"AttributeError: readonly attribute"


f('a', e='e')


It seems like we need to work with the globals we have aka. importing 
things locally.


Best,
Sven

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web