原文:http://thecodeship.com/patterns/guide-to-python-function-decorators/
仅此做一个中文翻译:
我们知道,python在方法def的使用上,有一些非常强大的功能。
譬如,将方法传给一个变量:
def sayHI(name): return "hi " + name ExexSayHI = sayHI print ExexSayHI("Allen")
或者在方法中定义方法
def SayHi(name): def hi(): return "hi " return hi() + name print SayHi("Allen")
方法也可以变成参数进行传递
def SayHi(name): return "Hi " + name def SayHiFrom(func): return func("Allen") print SayHiFrom(SayHi)
方法也可以变成返回值
def SayHi(name): def SayHiInner(): return "Hi " + name return SayHiInner() hi = SayHi print hi("Allen")
To be continue.....
时间: 2024-10-13 16:13:43