def outer_function():
print "1. This is outer function!"
def inner_function():
print "2. This is inner function, inside outer function!"
print "3. This is outside inner function, inside outer function!"
return inner_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!
class decoclass(object):
def __init__(self, f):
self.f = f
def __call__(self, *args, **kwargs):
# before f actions
print('decorator initialised')
self.f(*args, **kwargs)
print('decorator terminated')
# after f actions
@decoclass
def klass():
print('class')
klass()
###############################output#########################
decorator initialised
class
decorator 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: