python functions as objects

第5章

1.First-Class function

这里的first-class 的意思是        一个函数可以作为对象,在另一个函数中返回

 

metaclass programming中 也有 first-class class的概念,也就是说   类  可以作为对象,   在另外一个类中  创建,返回

 

 

2. 函数参数

 

 

 

 

def foo(a, b=1, *args, d, **kwargs):
    print(a, b, args, d, kwargs)


foo(1, 2, 3, 4, 5, d=6, e=7, f=8)

  

 

 

 

 

第6章

设计模式中   Strategy    和   Command   虽然很相似,但是关注点不一样

Strategy 模式 :

  注重算法的可替代性,算法可以封装成为一个函数对象,这个函数对象是可以更换的

 

Command 模式:

  涉及invoker  和  receiver

 

 

 

第七章

总感觉有点绕,看的懂,写的时候不会写,所以背下来这两种经典写法

 

不带参数的decorator的经典写法

def decoratorFunctionName(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        ***前置的代码***
        return func(*args, **kwargs)
    return wrapper

  

带参数的decorator的经典写法

def decoratorFunctionName(decoratorArguments):
    def decorator(func):
        @functools.wrapper(func)
        def wrapper(*args, **kwargs):
            ***前置的代码中可以使用decoratorArguments***
            return func(*args, **kwargs)
        return wrapper
    return decorator

  

 

posted @ 2018-05-09 12:06  eeechoo  阅读(159)  评论(0编辑  收藏  举报