相关疑难解决方法(0)

Python super()行为不可靠

出于某种原因,该super()方法并不总是按预期运行,选择返回:

TypeError('super(type, obj): obj must be an instance or subtype of type)'
Run Code Online (Sandbox Code Playgroud)

我理解错误的含义.我不明白为什么它会出现错误.这是破解的代码片段.系统中的所有对象都是新样式对象.

真正有趣的是,这个错误并不总是出现.我不知道是什么导致了它.该super()在方法Retrieval中传递Retrieval类,然后本身作为一个对象,它是,据我所知,究竟如何super()应该被调用.

有什么想法吗?

在文件DBConnection.py中:

class DBAdminConnection(object):
    def __init__(self):
        self.user = DBUserConnection().user 
        self.submissions = DBSubmissionConnection()
Run Code Online (Sandbox Code Playgroud)

在文件Retrieval.py中

class Retrieval(DBConnection.DBAdminConnection): 
    def __init__(self, username=None, password=None, unique_key=None):
        super(Retrieval,self).__init__()
        if username and password:
            self.username = username
            self.user.login(username,password, config.DATABASE)
            if self.user.error:
                raise UserLoginError(username)
        self.unique_key = unique_key
Run Code Online (Sandbox Code Playgroud)

python inheritance super superclass

41
推荐指数
2
解决办法
2万
查看次数

标签 统计

inheritance ×1

python ×1

super ×1

superclass ×1