折腾:
期间,需要去pyspider中,继承ResultWorker,然后要自定义初始化__init__
用: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)
结果:
super.__init__(resultdb, inqueue)
TypeError: descriptor ‘__init__’ requires a ‘super’ object but received a ‘ResultDB’
用: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__(self, resultdb, inqueue)
TypeError: descriptor ‘__init__’ requires a ‘super’ object but received a ‘AutohomeResultWorker’
python override __init__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"
【总结】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)
好像就可以实现继承了。