python - Is there a pythonic way to skip decoration on a subclass' method? -


i have class decorates methods using decorator library. specifically, class subclasses flask-restful resources, decorates http methods httpauth.httpbasicauth().login_required(), , sensible defaults on model service.

on subclasses want decorator applied; therefore i'd rather remove add in subclasses.

my thought have private method operations , public method decorated. effects of decoration can avoided overriding public method call private 1 , not decorating override. mocked example below.

i curious know if there's better way this. there shortcut 'cancelling decorators' in python gives effect?

or can recommend better approach?

some other questions have suitable answers this, e.g. is there way function decorator has wrapped?. question broader design - interested in any pythonic way run operations in decorated methods without effects of decoration. e.g. example 1 such way there may others.

def auth_required(fn):     def new_fn(*args, **kwargs):         print('auth required resource...')         fn(*args, **kwargs)     return new_fn  class resource:     name = none      @auth_required     def get(self):         self._get()      def _get(self):         print('getting %s' %self.name)  class eggs(resource):     name = 'eggs'  class spam(resource):     name = 'spam'      def get(self):         self._get()         # super(spam, self)._get()  eggs = eggs() spam = spam()  eggs.get() # auth required resource... # getting eggs  spam.get() # getting spam 

flask-httpauth uses functools.wraps in login_required decorator:

def login_required(self, f):     @wraps(f)     def decorated(*args, **kwargs):         ... 

from python 3.2, calls update_wrapper, can access original function via __wrapped__:

to allow access original function introspection , other purposes (e.g. bypassing caching decorator such lru_cache()), function automatically adds __wrapped__ attribute wrapper refers function being wrapped.

if you're writing own decorators, in example, can use @wraps same functionality (as keeping docstrings, etc.).

see is there way function decorator has wrapped?


Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -