类继承问题

Ben*_*enH 1 python urllib2 python-2.4

我正在尝试创建一个扩展HTTPBasicAuthHandler的类.出于某种原因,我在旧代码中使用的相同方法在这里不起作用.

class AuthInfo(urllib2.HTTPBasicAuthHandler):
    def __init__(self, realm, url, username, password):
        self.pwdmgr     = urllib2.HTTPPasswordMgrWithDefaultRealm()
        self.pwdmgr.add_password(None, url, username, password)
        super(AuthInfo, self).__init__(self.pwdmgr)
Run Code Online (Sandbox Code Playgroud)

这是错误:

Traceback (most recent call last):
  File "./RestResult.py", line 67, in ?
    auth = AuthInfo(None, "default", "xxxxx", "xxxxxxxx")
  File "./RestResult.py", line 47, in __init__
    super(AuthInfo, self).__init__(self.pwdmgr)
TypeError: super() argument 1 must be type, not classobj
Run Code Online (Sandbox Code Playgroud)

dan*_*ano 7

urllib2.HTTPBasicAuthHandler班是一个旧式类(不继承object),这意味着它不能被使用super.你必须__init__直接调用它:

urllib2.HTTPBasicAuthHandler.__init__(self, self.pwdmgr)
Run Code Online (Sandbox Code Playgroud)