0

我有一個Parser類,它具有解析文檔的方法。解析器就像解析器函數的「容器」。現在解析器還包含NDB map_async回調函數,以將其他參數傳遞給回調函數。使用回調,對象和繼承模式的應用引擎Python NDB map_async

類P爲每個映射的doc實體都有一個實例,它使用單個解析器實例來解析文檔。

class P (object): 

    def __init__(self, parser, ...): 
     self.parser = parser 
     .... 

    def get_content(...) 
     ....    # using methods from P and self.parser 

    def other methods ... 
     ....    # using methods from P and self.parser 

class Parser(object): 

    def __init__(args) 
     .... 

    def some parser methods() 
     .... 

    def callback(entity): # Is now a method of Parser, but .... 

     p = P(self, ...) 
     .... 
     content = p.get_content(...) 
     .... 

parser = Parser(...) # initialize Parser (instance, closure) 
query = Docs.query() 
multi_future = query.map_async(parser.callback) 
.... # process results 

這工作正常,但感覺不太好。

我尋找其中P從分析器繼承的溶液: (回調可以/應當是P的一部分)

query = Docs.query() 
multi_future = query.map_async(????? P ??????) 
.... # process results 

回答

1

我計算出來,使用的λ。

query = Docs.query() 
multi_future = query.map_async(lambda entity: P(...).callback(entity))