Ensuring Argument Type Decorator

  • Share this:

Code introduction


Defines a decorator that ensures the first argument is an instance of the second argument. If not, it raises a TypeError.


Technology Stack : Decorator, type checking

Code Type : Decorator

Code Difficulty : Intermediate


                
                    
def aaaa(arg1, arg2):
    def bbbb(func):
        def wrapper(*args, **kwargs):
            if isinstance(arg1, arg2):
                return func(*args, **kwargs)
            else:
                raise TypeError("arg1 must be an instance of arg2")
        return wrapper
    return bbbb