简体   繁体   中英

Use a base class method as a decorator in derived classes

I have come across a situation where I have a base class and multiple derived classes. In each of the derived classes I am doing an action of extracting common data from input given to method. Thus I have written that piece of code in base class as method so that derived classes could use it directly. But I want to call this function from each method where I want to perform this action. My question is that can I write a decorator for this action? I want this decorator to be a member of base class. Is it possible to use base class method as a decorator in derived classes?

Base class:

class BaseRequestHandler(object):

    def __init__(self):
        self.request=None
        self.response=None

    def extractheader(self,requestin):
        ##extracts header from incoming request

Derived class:

class DerivedRequestHandler1(BaseRequestHandler):

    def __init__(self):
        super(DerivedRequestHandler1, self).__init__()  

    def handlermethod(self,requestin)
        ##extract headed
        ##call internal API with header

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM