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


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

Re: monkey patching __code__

Started by"Sven R. Kunze" <srkunze@mail.de>
First post2016-03-18 15:33 +0100
Last post2016-03-18 11:01 -0700
Articles 2 — 2 participants

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:33 +0100
    Re: monkey patching __code__ Ned Batchelder <ned@nedbatchelder.com> - 2016-03-18 11:01 -0700

#105220 — Re: monkey patching __code__

From"Sven R. Kunze" <srkunze@mail.de>
Date2016-03-18 15:33 +0100
SubjectRe: monkey patching __code__
Message-ID<mailman.323.1458311618.12893.python-list@python.org>
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.

Thanks again. :-)

Again, why would it make sense for those dunder attributes to be part of 
the function but not of the code object?

Best,
Sven

[toc] | [next] | [standalone]


#105229

FromNed Batchelder <ned@nedbatchelder.com>
Date2016-03-18 11:01 -0700
Message-ID<f008250c-e8d7-47b1-b4e0-ce9e0d923ff3@googlegroups.com>
In reply to#105220
On Friday, March 18, 2016 at 10:33:46 AM UTC-4, 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.
> 
> Thanks again. :-)
> 
> Again, why would it make sense for those dunder attributes to be part of 
> the function but not of the code object?

Code objects are immutable, and marshal'able (a .pyc file is basically
just a magic-numbered, time-stamped, marshalled code object for the
module).  Marshal can't marshal arbitrary Python objects.

Functions are mutable, and constructed at run-time, so can reference
arbitrary Python objects.  Since you want default values to possibly
be user-defined Python objects, the defaults are on the function, not
the code object.

--Ned.

[toc] | [prev] | [standalone]


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


csiph-web