美文网首页
Callable对象动态生成函数

Callable对象动态生成函数

作者: GradientDescent | 来源:发表于2017-11-28 11:44 被阅读0次

    --- 2017.11.28

    动机

    有一批相似的函数,代码逻辑是重复的,只有几个参数是不同的;比如在bigflow里输出counter,counter.increase("my_counter_a") 需要定义成函数:

    def my_counter_a(r):
        counter.increase("my_counter_a")
        return r
    
    my_pcollection.map(my_counter_a)
    

    当要输出很多counter时,就很烦了。

    解法

    可以定义一个Callable对象,在运行时生成函数对象(其实类似C++里的仿函数的用法):

    from collections import Callable
    from bigflow import counter
    
    class Counter(Callable):
        def __init__(self, counter_name):
            self.counter_name = counter_name
    
        def __call__(self, record):
            counter.increase(self.counter_name)
            return record
    
    my_pcollection.map(Counter("my_counter_a"))
    

    collections.Callable 的实现在 lib/python2.7/_abcoll.py, 使用了 metaclass,后续研究一下再补充。

    ---2017.11.29
    今天发现Callable对象跟function还是不一样,不能用在pcollection.map()里;

    又想了另一个方法:

    def _do_counter(record, counter_name):
         counter.increase(counter_name)
         return record
    

    相关文章

      网友评论

          本文标题:Callable对象动态生成函数

          本文链接:https://www.haomeiwen.com/subject/iipsbxtx.html