What does functools.wraps do?

前端 未结 6 838
北海茫月
北海茫月 2020-11-22 06:23

In a comment on this answer to another question, someone said that they weren\'t sure what functools.wraps was doing. So, I\'m asking this question so that ther

6条回答
  •  青春惊慌失措
    2020-11-22 06:56

    I very often use classes, rather than functions, for my decorators. I was having some trouble with this because an object won't have all the same attributes that are expected of a function. For example, an object won't have the attribute __name__. I had a specific issue with this that was pretty hard to trace where Django was reporting the error "object has no attribute '__name__'". Unfortunately, for class-style decorators, I don't believe that @wrap will do the job. I have instead created a base decorator class like so:

    class DecBase(object):
        func = None
    
        def __init__(self, func):
            self.__func = func
    
        def __getattribute__(self, name):
            if name == "func":
                return super(DecBase, self).__getattribute__(name)
    
            return self.func.__getattribute__(name)
    
        def __setattr__(self, name, value):
            if name == "func":
                return super(DecBase, self).__setattr__(name, value)
    
            return self.func.__setattr__(name, value)
    

    This class proxies all the attribute calls over to the function that is being decorated. So, you can now create a simple decorator that checks that 2 arguments are specified like so:

    class process_login(DecBase):
        def __call__(self, *args):
            if len(args) != 2:
                raise Exception("You can only specify two arguments")
    
            return self.func(*args)
    

提交回复
热议问题