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)

posted @ 2017-08-28 09:43  开始战斗  阅读(643)  评论(0编辑  收藏  举报