defouter_function():print"1. This is outer function!"definner_function():print"2. This is inner function, inside outer function!"print"3. This is outside inner function, inside outer function!"returninner_function()func_assign =outer_function()#######################Output######################################1. This is outer function!3. This is outside inner function, inside outer function!2. This is inner function, inside outer function!
deftest(f): print"before ..."f()print"after ..."@testdeffunc(): print"func was called"#############################output###################################before ... #func was called #after ...
#!/usr/bin/python3# 类中方法的修饰符,在不需要找到调用函数时改写类的属性# python中一切皆对象,可以通过修饰符以函数为参数调用修饰器函数,# 传入的函数一定是可调用的 callabled defmethod_decor(method):definner(city):if city.name =='LosA':print('It\'s a great city!')else:method(city)return innerclassCity(object):def__init__(self,name): self.name = name@method_decordefprint_test(self):print(self.name)c1 =City('LosA')c1.print_test()c2 =City('Beijing')c2.print_test()####################output#####################################It's a great city!Beijing
classdecoclass(object):def__init__(self,f): self.f = fdef__call__(self,*args,**kwargs):# before f actionsprint('decorator initialised') self.f(*args, **kwargs)print('decorator terminated')# after f actions@decoclassdefklass():print('class')klass()###############################output#########################decorator initialisedclassdecorator terminated
五.多装饰器同时操作 (Chain Decorators)
A function definition may be wrapped by one or more decorator expressions. Decorator expressions are evaluated when the function is defined, in the scope that contains the function definition. The result must be a callable, which is invoked with the function object as the only argument. The returned value is bound to the function name instead of the function object. Multiple decorators are applied in nested fashion. For example, the following code:
#!/usr/bin/python3defdecorator(func):"""decorator docstring is here!"""definner(*args):"""inner docstring is here!"""print(func.__name__+' was called')returnfunc(*args)return inner@decoratordeffunc(x):"""Trible times"""return x**3print(func(3))print(func.__name__)print(func.__doc__)
#!/usr/bin/python3from functools import wrapsdefdecorator(func):"""decorator docstring is here!"""@wraps(func)definner(*args):"""inner docstring is here!"""print(func.__name__+' was called')returnfunc(*args)return inner@decoratordeffunc(x):"""Trible times"""return x**3print(func(3))print(func.__name__)print(func.__doc__)#####################################output###############################func was called27funcTrible times
七.向装饰器传递参数
#!/usr/bin/python3classDecorator(object):def__init__(self,arg1,arg2):print('参数传递给装饰器类,分别为:{0} and {1}'.format(arg1, arg2)) self.arg1 = arg1 self.arg2 = arg2def__call__(self,func,*args,**kwargs):definner(*args,**kwargs):print('参数传递给内部方法:%s and %s'% (self.arg1, self.arg2))returnfunc(*args, **kwargs)return inner@Decorator('kobe', 'Brany')defprint_(*args):for arg in args:print(arg, end='')print_(1,2,3)参数传递给装饰器类,分别为:kobe and Brany参数传递给内部方法:kobe and Brany123