首页 > 代码库 > python closure and function decorators 1

python closure and function decorators 1

原文:http://thecodeship.com/patterns/guide-to-python-function-decorators/

仅此做一个中文翻译:

我们知道,python在方法def的使用上,有一些非常强大的功能。

譬如,将方法传给一个变量:

def sayHI(name):    return "hi " + name    ExexSayHI = sayHIprint ExexSayHI("Allen")

或者在方法中定义方法

def SayHi(name):    def hi():        return "hi "    return hi() + nameprint SayHi("Allen")

方法也可以变成参数进行传递

def SayHi(name):    return "Hi " + namedef SayHiFrom(func):    return func("Allen")    print SayHiFrom(SayHi) 

方法也可以变成返回值

def SayHi(name):    def SayHiInner():        return "Hi " + name        return SayHiInner()hi = SayHiprint hi("Allen")

 

To be continue.....

 

  

 

python closure and function decorators 1