我需要从SUDS模块扩展类Client ...例如,我有一个简单的代码,它可以很好地工作
client = Client(wsdl, username = USERNAME, password = PASSWORD, headers = {'Content-Type': 'application/soap+xml'}, plugins = [VapixHelper()])
rules = client.service.GetActionRules()所以我需要为这个类添加一些额外的方法,所以我尝试这样做:
class Vapix(Client):
def __init__(self, args):
globals().update(vars(args))
USERNAME, PASSWORD = user_data.split(':')
super(Vapix, self).__init__(wsdl, username = USERNAME, password = PASSWORD, headers = {'Content-Type': 'application/soap+xml'}, plugins = [VapixHelper()])
def setActionStatus(self, status):
print super(Vapix, self).service.GetActionRules()我得到的是这个错误而不是结果:
Traceback (most recent call last):
File "vapix.py", line 42, in <module>
client.setActionStatus(True)
File "vapix.py", line 36, in setActionStatus
print super(Vapix, self).service.GetActionRules()
AttributeError: 'super' object has no attribute 'service'发布于 2014-03-28 15:52:41
您没有覆盖service()方法,因此您不需要使用super()来查找原始方法;相反,删除super()调用并直接访问self上的属性:
def setActionStatus(self, status):
print self.service.GetActionRules()只有当您需要搜索基类(按方法解析顺序)以查找方法(或其他描述符对象)时,才需要super(),这通常是因为当前类已经重新定义了该名称。
如果需要调用基类foo,但当前类实现了foo方法,则不能使用self.foo(),而需要使用super()。例如,您必须为__init__使用super();您的派生类有自己的__init__方法,因此调用self.__init__()将递归地调用该方法,但super(Vapix, self).__init__()可以工作,因为super()会查看self的MRO,以该顺序找到Vapix,然后查找具有__init__方法的下一个类。
在这里,service是一个实例属性;它直接在self上定义,甚至不是一个方法。
https://stackoverflow.com/questions/22706965
复制相似问题