Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python中函数修饰符的变体_Python_Decorator - Fatal编程技术网

Python中函数修饰符的变体

Python中函数修饰符的变体,python,decorator,Python,Decorator,Python中函数类型和类类型、参数和无参数修饰符的不同体现有哪些示例?请看这里:请看这里: def decorator_noarg(f): def wrapper(*args, **kwargs): print("Inside decorator-function, WITHOUT arg.") return f(*args, **kwargs) return wrapper def decorator_witharg(dec_arg):

Python中函数类型和类类型、参数和无参数修饰符的不同体现有哪些示例?

请看这里:请看这里:
def decorator_noarg(f):
    def wrapper(*args, **kwargs):
        print("Inside decorator-function, WITHOUT arg.")
        return f(*args, **kwargs)
    return wrapper

def decorator_witharg(dec_arg):
    def real_decorator(f):
        def wrapper(*args, **kwargs):
            print("Inside decorator-function, WITH arg: %s" % (dec_arg))
            return f(*args, **kwargs)
        return wrapper
    return real_decorator

class decoratorclass_noarg(object):
    f = None
    def __init__(self, f):
        def host_wrapper(*arg, **kwargs):
            print("Inside decorator-class, WITHOUT arg.")

            # We lose the original instance of the object.
            return f(test_class(), *arg, **kwargs)

        self.f = host_wrapper

    def __call__(self, *args, **kargs):
        return self.f(*args, **kargs)

class decoratorclass_witharg(object):
    f = None
    def __init__(self, dec_arg):
        def decorator(host_method):
            def host_wrapper(host_obj, *arg, **kwargs):
                print("Inside decorator-class, WITH arg: %s" % (dec_arg))
                return host_method(host_obj, *arg, **kwargs)
            return host_wrapper
        self.f = decorator

    def __call__(self, *args, **kwargs):
        return self.f(*args, **kwargs)

class decorator_on_function_noarg(object):
    f = None

    def __init__(self, f):
        self.f = f
        pass

    def __call__(self):
        print("Inside decorator-on-function class, without arg.")
        return self.f()

class decorator_on_function_witharg(object):
    f = None

    def __init__(self, dec_arg):
        def decorator(host_method):
            def host_wrapper(*arg, **kwargs):
                print("Inside decorator-on-function class, with arg: %s" % (dec_arg))
                return host_method(*arg, **kwargs)
            return host_wrapper
        self.f = decorator

    def __call__(self, *args, **kwargs):
        return self.f(*args, **kwargs)

def decorator_function_on_method_noarg(f):
    print("Inside decorator-function on method, without arg.")
    return f

def decorator_function_on_method_witharg(dec_arg):
    def wrapper(f):
        print("Inside decorator-function on method, with arg: %s" % (dec_arg))
        return f

# Testing calls.

class test_class(object):
    #@decorator_witharg(44)
    #@decorator_noarg
    #@decoratorclass_witharg(1)
    #@decoratorclass_noarg
    #@decorator_function_on_method_noarg
    #@decorator_function_on_method_witharg(33)
    def test_func(self, arg1, arg2):
        print("Inside: %s, %s" % (arg1, arg2))
        return 123

#@decorator_on_function_noarg
#@decorator_on_function_witharg(22)
def test_func():
    print("Inside test_func.")

#test_class().test_func(33, 44)
#test_func()