nixawk/hello-python3

python decorator

nixawk opened this issue · 0 comments

$ cat evil_decorator.py
#!/usr/bin/python
# -*- coding: utf-8 -*-

import logging
import functools


logging.basicConfig(level=logging.INFO)
log = logging.getLogger(__name__)


def evil_decorator(func):
    print('evil code')
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        return func(*args, **kwargs)
    return wrapper


@evil_decorator
def say_hello(name):
    print("hello {}".format(name))
    return name


if __name__ == '__main__':
    pass
Python 3.6.0
$ python evil_decorator.py
evil code
$ python2.7 evil_decorator.py
evil code

References

  1. https://stackoverflow.com/questions/18062443/python-decorator-function-called-at-compile-time
  2. https://stackoverflow.com/questions/341379/python-decorators-run-before-function-it-is-decorating-is-called