confusion with decorators

Chris Angelico rosuav at gmail.com
Thu Jan 31 10:28:41 EST 2013


On Fri, Feb 1, 2013 at 12:25 AM, Jason Swails <jason.swails at gmail.com> wrote:
> On Thu, Jan 31, 2013 at 12:46 AM, Steven D'Aprano
> <steve+comp.lang.python at pearwood.info> wrote:
>>
>> On Wed, 30 Jan 2013 19:34:03 -0500, Jason Swails wrote:
>>
>> > Hello,
>> >
>> > I was having some trouble understanding decorators and inheritance and
>> > all that.  This is what I was trying to do:
>> >
>> > # untested
>> > class A(object):
>> >    def _protector_decorator(fcn):
>> >       def newfcn(self, *args, **kwargs):
>> >          return fcn(self, *args, **kwargs)
>> >       return newfcn
>>
>> Well, that surely isn't going to work, because it always decorates the
>> same function, the global "fcn".
>
>
> I don't think this is right.  fcn is a passed function (at least if it acts
> as a decorator) that is declared locally in the _protector_decorator scope.
> Since newfcn is bound in the same scope and fcn is not defined inside
> newfcn, I'm pretty sure that newfcn will just grab the fcn passed into the
> decorator.

Yet it adds a level of indirection that achieves nothing. Why not simply:
def _protector_decorator(fcn):
  return fcn

? I'm not understanding the purpose here.

ChrisA



More information about the Python-list mailing list