python的语法糖
# -*- coding: utf-8 -*-
def deco(func):
print("before myfunc() called.")
func()
print("after myfunc() called.")
return func
bool=0
@deco # 从这里可以看出@time 等价于 time(xxx()),但是这种写法你得考虑python代码的执行顺序
def myfunc():
global bool
bool=bool+1
if not bool==2:
print("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
bool=1
myfunc()
不用语法糖实现该功能
# -*- coding: utf-8 -*-
def deco(func):
print("before myfunc() called.")
func()
print("after myfunc() called.")
return func
bool=0
# @deco
def myfunc():
global bool
bool=bool+1
if not bool==2:
print("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
bool=1
# myfunc()
deco(myfunc)
传参
def bar(*param1, **param2):
print param1
print param2
def foo(bar, *param1, **param2):
bar(*param1, **param2)
foo(bar, 1, 2, 3, a = 111, b = 222, c = 333)