Python super()行为不可靠 [英] Python super() behavior not dependable
问题描述
出于某种原因, super()
方法并不总是按预期运行,选择返回:
For some reason, the super()
method is not always behaving as expected, opting to return:
TypeError('super(type, obj): obj must be an instance or subtype of type)'
我理解错误意味着什么。我不明白为什么它会出现错误。这是破解的代码片段。系统中的所有对象都是新样式对象。
I understand what the error means. I do not understand why it is coming up as an error. Here's the snippet of code that is breaking. All objects in the system are new style objects.
真正有趣的是这个错误总是显示出来。我不知道是什么导致了它。 Retrieval
中的 super()
方法正在传递 Retrieval
class,然后将它自己作为一个对象,据我所知,究竟是如何调用 super()
假设 。
What's really interesting is that this error does not always show up. I don't know what's causing it. The super()
method in Retrieval
is passing the Retrieval
class, and then itself as an object, which is, as far as I'm aware,exactly how super()
is supposed to be invoked.
有任何想法吗?
在文件 DBConnection.py 中:
class DBAdminConnection(object):
def __init__(self):
self.user = DBUserConnection().user
self.submissions = DBSubmissionConnection()
在文件 Retrieval.py中/ em>
In file 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
推荐答案
你是否正在以某种方式重新加载模块?如果是这样,那可以解释这个错误。
Are you reloading modules somehow in the middle of things? If so, that may explain this error.
isinstance(self,DBAdminConnection)
在重新加载模块后可能会变为false显然,内存引用的更改。
isinstance(self,DBAdminConnection)
may become false after reloading modules because of the changes to memory references, apparently.
编辑:如果你在mod_wsgi下运行你的web.py应用程序,请确保你禁用自动重载:
if you're running your web.py app under mod_wsgi, make sure you're disabling autoreload:
app = web.application(urls, globals(), autoreload=False)
这篇关于Python super()行为不可靠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!