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


Groups > comp.lang.python > #49913

Re: Decorator help

References <fdc5f317fd454a4dbb9b3033dbe18a60@exch.activenetwerx.com> <CAN1F8qX0KVYYE6=M2hS8DXZJUN3tTG+Uk6c8HTsZkNKMrFcj_w@mail.gmail.com> <CAN1F8qXneccUCs+8cs-7w1yStSrdsaZS8Dg3V2f3BgegiATofA@mail.gmail.com> <kr31pu$l74$1@ger.gmane.org>
From Joshua Landau <joshua.landau.ws@gmail.com>
Date 2013-07-05 02:14 +0100
Subject Re: Decorator help
Newsgroups comp.lang.python
Message-ID <mailman.4259.1372986890.3114.python-list@python.org> (permalink)

Show all headers | View raw


On 4 July 2013 06:39, Peter Otten <__peter__@web.de> wrote:
> Joshua Landau wrote:
>
>> On 3 July 2013 23:19, Joshua Landau <joshua.landau.ws@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.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Decorator help Joshua Landau <joshua.landau.ws@gmail.com> - 2013-07-05 02:14 +0100

csiph-web