回調函數用起來比較爽。特別是在js中,滿世界全是回調,那麼在python中,怎麼來優雅地實現本身的回調函數呢python
下面貼一個我寫的例子app
class BaseHandler(object): def crawl(self, url, **kwargs): if kwargs.get('callback'): callback = kwargs['callback'] if isinstance(callback, basestring) and hasattr(self, callback): func = getattr(self, callback) elif hasattr(callback, 'im_self') and callback.im_self is self: func = callback resp = requests.get(url) return func(resp) class Anjuke(BaseHandler): def on_start(self): self.crawl(start_url, callback=self.index_url) def index_url(self, response): print response.text a = Anjuke() print a.on_start()
在這裏實現的是當self.crawl指定callback方法時,self.crawl會在接收到start_url參數執行完成後回調self.index_url。函數
而實現方式其實就是經過kwargs獲取到callback的對象/字符串,而後經過getattr(self, callback)來反射獲得回調的方法,而後調用便可。ui
接着能夠進一步抽象,增長回調方法的複用,因而BaseHandler能夠改寫成裝飾器方式url
def callback(f): def wrapper(obj, *args, **kwargs): resp = f(obj, *args, **kwargs) if kwargs.get('callback'): mcallback = kwargs['callback'] if isinstance(callback, basestring) and hasattr(obj, mcallback): func = getattr(obj, mcallback) elif hasattr(mcallback, 'im_self') and mcallback.im_self is obj: func = mcallback kwargs['callback'] = func.__name__ else: raise NotImplementedError("self.%s() not implemented!" % callback) return func(resp) return wrapper class BaseHandler(object): """在self.crawl(url,callback=$func)時,經過裝飾器callback便可實現回調,在self.crawl方法中,只須要專一於獲得$func的參數便可""" @callback def crawl(self, url, **kwargs): url = quote_chinese(_build_url(url.strip(), kwargs.get('params'))) return requests.get(url)