Decorator help

Joshua Landau joshua.landau.ws at gmail.com
Fri Jul 5 03:14:02 CEST 2013


On 4 July 2013 06:39, Peter Otten <__peter__ at web.de> wrote:
> Joshua Landau wrote:
>
>> On 3 July 2013 23:19, Joshua Landau <joshua.landau.ws at gmail.com> wrote:
>>> If you don't want to do that, you'd need to use introspection of a
>>> remarkably hacky sort. If you want that, well, it'll take a mo.
>>
>> After some effort I'm pretty confident that the hacky way is impossible.
>
> Well, technically it's
>
> func.func_closure[0].cell_contents.__name__
>
> but of course you cannot know that for the general case.

I didn't want to do something like that as it implies a lot of
knowledge about the function -- which implies that there's no reason
to do it hacky in the first place. I was using
"inspect.getclosurevars(func).nonlocals" and that "coerces" to a
dictionary first. It's the "correct" way of doing things. But you
never know what name the function inside the wrapper is bound to, so I
didn't accept that.

Also, your method has undefined behaviour AFAIK -- the order of
func_closure is compiler-dependant. If you want to do something like
this, I recommend my method (but it doesn't work for the general case
in the slightest):

    inspect.getclosurevars(func).nonlocals["func"].__name__

If you can't assume the name it's stored in, but you can know the
order of closure variables *then* use Peter's. But again, don't use
either; it's impossible just as I said.



More information about the Python-list mailing list