monkey patching __code__

Ian Kelly ian.g.kelly at gmail.com
Fri Mar 18 10:48:01 EDT 2016


On Mar 18, 2016 8:33 AM, "Sven R. Kunze" <srkunze at mail.de> wrote:
>
> On 18.03.2016 14:47, Ian Kelly wrote:
>>
>> Your patched version takes two extra arguments. Did you add the
>> defaults for those to the function's __defaults__ attribute?
>
>
> That's it! :-) Thanks a lot.
>
> Just to understand this better: why is that not part of the code object
but part of the function?

Well I didn't design it, so I'm not really sure. But it could be argued
that the defaults are intrinsic to the function declaration, not the code
object, as not all code objects even have arguments. It also makes it
straight-forward to create a new function that uses the same code but with
different defaults or globals.

>> This sounds like a pretty hairy thing that you're trying to do. Surely
>> there must be some better way to accomplish the same goal.
>
>
> We are open for suggestions. We featured our own reverse function for a
while but it lead to inconsistent behaviors across the field. Especially
considering that Django provides an {% url %} template tag which would then
use yet another reverse implementation.

I don't really have a good suggestion. I was mostly hoping that Django
provided a way to hook in a different implementation.



More information about the Python-list mailing list