码迷,mamicode.com
首页 > 编程语言 > 详细

python closure and function decorators 1

时间:2014-12-03 20:47:24      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   io   color   os   使用   sp   

原文: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.....

 

  

 

python closure and function decorators 1

标签:des   style   blog   http   io   color   os   使用   sp   

原文地址:http://www.cnblogs.com/allencharp/p/4141082.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!