折腾:
【已解决】pyspider中出错:TypeError __init__() got an unexpected keyword argument resultdb
期间,需要去pyspider中,继承ResultWorker,然后要自定义初始化__init__
用:
<code>class AutohomeResultWorker(ResultWorker): mysqldb = None def __init__(self, resultdb, inqueue): """init mysql db""" print("AutohomeResultWorker init") print("resultdb=%s, inqueue=%s" % (resultdb, inqueue)) #super.__init__(self, resultdb, inqueue) super.__init__(resultdb, inqueue) if self.mysqldb is None: self.mysqldb = MysqlDb() print("self.mysqldb=%s" % self.mysqldb) </code>
结果:
super.__init__(resultdb, inqueue)
TypeError: descriptor ‘__init__’ requires a ‘super’ object but received a ‘ResultDB’
用:
<code>def __init__(self, resultdb, inqueue): """init mysql db""" print("AutohomeResultWorker init") print("resultdb=%s, inqueue=%s" % (resultdb, inqueue)) super.__init__(self, resultdb, inqueue) </code>
结果:
super.__init__(self, resultdb, inqueue)
TypeError: descriptor ‘__init__’ requires a ‘super’ object but received a ‘AutohomeResultWorker’
python override __init__
override – Inheritance and Overriding __init__ in python – Stack Overflow
__init__ method calls overridden method – Python rules – Semmle
<code>class Super(object): def __init__(self, arg): self._state = "Not OK" self.super_set_up(arg) self._state = "OK" def super_set_up(self, arg): "Do some set up" class Sub(Super): def __init__(self, arg): Super.__init__(self, arg) self.sub_set_up(self, arg) self.important_state = "OK" def sub_set_up(self, arg): "Do some more set up" </code>
【总结】
<code>from pyspider.result import ResultWorker class AutohomeResultWorker(ResultWorker): def __init__(self, resultdb, inqueue): """init mysql db""" print("AutohomeResultWorker init") print("resultdb=%s, inqueue=%s" % (resultdb, inqueue)) ResultWorker.__init__(self, resultdb, inqueue) # print("self.mysqldb=%s" % (self.mysqldb)) # if self.mysqldb is None: self.mysqldb = MysqlDb() print("self.mysqldb=%s" % self.mysqldb) </code>
好像就可以实现继承了。