回调函数用起来比较爽。特别是在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)来反射得到回调的方法,然后调用即可。
时间: 2024-11-04 21:16:43