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


Groups > comp.lang.python > #37974

confusion with decorators

Date 2013-01-30 19:34 -0500
Subject confusion with decorators
From Jason Swails <jason.swails@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.1239.1359592452.2939.python-list@python.org> (permalink)

Show all headers | View raw


[Multipart message — attachments visible in raw view] - view raw

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.

Thanks!
Jason

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


Thread

confusion with decorators Jason Swails <jason.swails@gmail.com> - 2013-01-30 19:34 -0500
  Re: confusion with decorators Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-01-31 05:46 +0000
    Re: confusion with decorators Jason Swails <jason.swails@gmail.com> - 2013-01-31 08:25 -0500
      Re: confusion with decorators Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-01 10:16 +1100
        Re: confusion with decorators Jason Swails <jason.swails@gmail.com> - 2013-01-31 22:13 -0500
    Re: confusion with decorators Chris Angelico <rosuav@gmail.com> - 2013-02-01 02:28 +1100
    Re: confusion with decorators Jason Swails <jason.swails@gmail.com> - 2013-01-31 11:00 -0500
    Re: confusion with decorators Jason Swails <jason.swails@gmail.com> - 2013-01-31 12:53 -0500
    Re: confusion with decorators Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-01 08:31 +1100
  Re: confusion with decorators 88888 Dihedral <dihedral88888@googlemail.com> - 2013-02-01 03:26 -0800
  Re: confusion with decorators 88888 Dihedral <dihedral88888@googlemail.com> - 2013-02-01 03:26 -0800

csiph-web