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


Groups > comp.lang.python > #37998

Re: confusion with decorators

References <mailman.1239.1359592452.2939.python-list@python.org> <510a053a$0$11104$c3e8da3@news.astraweb.com> <CAEk9e3rbH8Jt9pL4zS=g5uEJZiRiPKDP=cMzaC7D-c2dnMBsMw@mail.gmail.com> <CAPTjJmqLQ-L=Z_e4es2=34DMCLK4ZcJpmia4v2udipTTsQeCvw@mail.gmail.com>
Date 2013-01-31 11:00 -0500
Subject Re: confusion with decorators
From Jason Swails <jason.swails@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.1251.1359648062.2939.python-list@python.org> (permalink)

Show all headers | View raw


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

On Thu, Jan 31, 2013 at 10:28 AM, Chris Angelico <rosuav@gmail.com> wrote:

>
> >> 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.
>

Bad example.  A better (longer) one that is closer to my true use-case:


from somewhere.exceptions import MyTypeError
from somewhere.different import AuthorClass, RemoteAuthorClass
from urllib2 import HTTPError

class A(object):

   authorclass = AuthorClass

   def __init__(self, obj_list):
      """
      Instantiate a list of obj_list objects that may have an "author"
      attribute
      """
      self.things = []
      for o in obj_list:
         if not isinstance(o, self.authorclass):
            raise MyTypeError('Bad type given to constructor')
         self.things.append(o)

   def _protector(fcn):
      def newfcn(self, *args, **kwargs):
         try:
            return fcn(self, *args, **kwargs) # returns a string
         except AttributeError:
            return 'Attribute not available.'
         except IndexError:
            return 'Not that many AuthorClasses loaded'

      return newfcn

   @_protector
   def author(self, idx):
      return self.things[idx].author

   @_protector
   def description(self, idx):
      return self.things[idx].description

   @_protector
   def hobbies(self, idx):
      return self.things[idx].hobbies

class B(A):

   authorclass = RemoteAuthorClass

   def _protector(fcn):
      def newfcn(self, *args, **kwargs):
         try:
            return fcn(self, *args, **kwargs)
         except AttributeError:
            return 'Attribute not available'
         except IndexError:
            return 'Not that many RemoteAuthorClasses loaded'
         except HTTPError:
            return 'Could not connect'
      return fcn

Basically, while RemoteAuthorClass and AuthorClass are related (via
inheritance), the RemoteAuthorClass has the potential for HTTPError's now.
 I could just expand the A class decorator to catch the HTTPError, but
since that should not be possible in AuthorClass, I'd rather not risk
masking a bug.  I'm under no impressions that the above code will decorate
A-inherited functions with the B-decorator (I know it won't), but that's
the effect I'm trying to achieve...

Thanks!
Jason

-- 
Jason M. Swails
Quantum Theory Project,
University of Florida
Ph.D. Candidate
352-392-4032

Back to comp.lang.python | Previous | NextPrevious in thread | Next 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