Python decorator cheat

  • A decorator without arguments
def decorator(f):
    @functools.wraps(f)
    def wrapped(*args, **kwargs):
        results = f(*args, **kwargs)
        # do something
        return results
    return wrapped
  • A Decorator with arguments
def decorator_args(a, b, c):
    def decorator(f):
        @functools.wraps(f)
        def wrapped(*args, **kwargs):
            results = f(*args, **kwargs)
            # do something
            return results
        return wrapped
    return decorator
Copyright © 2016 - 2019 caimaoy. All Rights Reserved            本站总访问量

results matching ""

    No results matching ""