Search code examples
pythondecoratorwrapperfunctools

Try each function of a class with functools.wraps decorator


I'm trying to define a decorator in order to execute a class method, try it first and, if an error is detected, raise it mentioning the method in which failed, so as to the user could see in which method is the error.

Here I show a MRE (Minimal, Reproducible Example) of my code.

from functools import wraps

def trier(func):
    """Decorator for trying A-class methods"""
    @wraps(func)
    def inner_func(self, name, *args):
        
        try:
            func(self, *args)
        
        except:
            print(f"An error apeared while {name}")
    
    return inner_func
    
class A:
    def __init__(self):
        self._animals = 2
        self._humans = 5
    
    @trier('getting animals')
    def animals(self, num):
        return self._animals + num
    
    @trier('getting humans')
    def humans(self):
        return self._humans

A().animals

Many errors are raising, like:

TypeError: inner_func() missing 1 required positional argument: 'name'

or misunderstanding self class with self function.


Solution

  • As an alternative to Stefan's answer, the following simply uses @trier without any parameters to decorate functions, and then when printing out the error message we can get the name with func.__name__.

    from functools import wraps
    
    def trier(func):
        """Decorator for trying A-class methods"""
        @wraps(func)
        def inner_func(self, *args, **kwargs):
    
            try:
                return func(self, *args, **kwargs)
    
            except:
                print(f"An error apeared in {func.__name__}")
    
        return inner_func
    
    class A:
        def __init__(self):
            self._animals = 2
            self._humans = 5
    
        @trier
        def animals(self, num):
            return self._animals + num
    
        @trier
        def humans(self):
            return self._humans
    
    print(A().animals(1))
    

    I also fixed a couple of bugs in the code: In trier's try and except the result of calling func was never returned, and you need to include **kwargs in addition to *args so you can use named parameters. I.e. A().animals(num=1) only works when you handle kwargs.