[Python-ideas] Access to function objects

Guido van Rossum guido at python.org
Tue Aug 9 02:55:55 CEST 2011

On Mon, Aug 8, 2011 at 8:42 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> Ben Finney wrote:
>> The ability to access the undecorated function would be of great help in
>> our Django-based project. Django is very decorator-happy, and we are
>> trying to introspect code and, given a particular URL, determine the
>> innermost function (more precisely, what location in our code) that
>> actually handles that URL.
>> Is that a use-case of interest?
> I shouldn't think the proposed __func__ local would be of any use here,
> because it is local to the function. As I understand your use-case, you need
> to inspect a function from the outside, where __func__ is not available, and
> peel back all the decorators to the innermost function.
> To do that, I would start by looking at:
> function.__closure__[0].cell_contents
> and recurse as necessary.

AFAIK there is no fool-proof way to peel back a decorator; decorators
aren't required to have any specific implementation.

If the use case was important it would reopen the question of always
making this value available as a frame attribute. But I'm not sure why
the line number indicated by the code object (which *is* accessible)
is insufficient.

--Guido van Rossum (python.org/~guido)

More information about the Python-ideas mailing list