回调函数用起来比较爽。特别是在js中,满世界全是回调,那么在python中,怎么来优雅地实现自己的回调函数呢
下面贴一个我写的例子
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()
其实就是通过kwargs获取到callback的对象/字符串,然后通过getattr(self, callback)来反射得到回调的方法,然后调用即可。
接着可以进一步抽象,增加回调方法的复用,于是BaseHandler可以改写成装饰器方式
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): @callback def crawl(self, url, **kwargs): url = quote_chinese(_build_url(url.strip(), kwargs.get('params'))) return requests.get(url)
到此这篇关于python构建自定义回调函数详解就介绍到这了。有一位青年去拜访一位很知名的科学家,向他诉说自己是如何刻苦努力,却还没有什么建树,科学家拿出一只放大镜,一张白纸,放在阳光下,纸上出现一个耀眼的光斑,片刻间就燃烧起来,年轻人茅塞顿开,欣然离去。更多相关python构建自定义回调函数详解内容请查看相关栏目,小编编辑不易,再次感谢大家的支持!