Re: confusion with decorators

Discussion in 'Python' started by Dave Angel, Jan 31, 2013.

  1. Dave Angel

    Dave Angel Guest

    On 01/30/2013 07:34 PM, 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
    > @_protector_decorator
    > def my_method(self, *args, **kwargs):
    > """ do something here """
    > class B(A):
    > def _protector_decorator(fcn):
    > def newfcn(self, *args, **kwargs):
    > raise MyException('I do not want B to be able to access the
    > protected functions')
    > return newfcn
    > The goal of all that was to be able to change the behavior of my_method
    > inside class B simply by redefining the decorator. Basically, what I want
    > is B.my_method() to be decorated by B._protector_decorator, but in the code
    > I'm running it's decorated by A._protector_decorator.
    > I presume this is because once the decorator is applied to my_method in
    > class A, A.my_method is immediately bound to the new, 'decorated' function,
    > which is subsequently inherited (and not decorated, obviously), by B.
    > Am I correct here? My workaround was to simply copy the method from class
    > A to class B, after which B._protector_decorator decorated the methods in
    > B. While this doesn't make the use of decorators completely pointless (the
    > decorators actually do something in each class, it's just different), it
    > does add a bunch of code duplication which I was at one point hopeful to
    > avoid.
    > I'm still stumbling around with decorators a little, but this exercise has
    > made them a lot clearer to me.

    I'm certainly not the expert on decorators; I've only used them for
    simple things. But I think I can clear up one misconception.

    The decorator function will execute while *compiling* the class A, and
    the one in class B is unreferenced.

    The decorator @_protector_decorator is shorthand for something like
    mymethod = _protector_decorator(mymethod)

    So by the time the compiler ends with class A, the mymethod has its
    final value.

    (Note, I've not used a decorator that was defined inside a class, so I'm
    probably missing the appropriate A. or self. or cls. overrides.)
    But the order of definition is still correct. A decorator executes
    once, just after a function is completed.

    Dave Angel, Jan 31, 2013
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael Strorm
    Michael Strorm
    Apr 20, 2005
  2. Michael Sparks

    Using metaclasses to play with decorators.

    Michael Sparks, Jun 15, 2004, in forum: Python
    Michele Simionato
    Jun 18, 2004
  3. Arien Malec

    PEP 318 decorators are not Decorators

    Arien Malec, Aug 13, 2004, in forum: Python
    Arien Malec
    Aug 16, 2004
  4. Henrik Faber

    Confusion about decorators

    Henrik Faber, Dec 12, 2011, in forum: Python
    Henrik Faber
    Dec 12, 2011
  5. Jason Swails

    confusion with decorators

    Jason Swails, Jan 31, 2013, in forum: Python
    88888 Dihedral
    Feb 1, 2013

Share This Page