添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

super(type, obj): obj必须是type的一个实例或子类型。

63 人关注

我为什么会出现以下错误,如何解决?

TypeError: super(type, obj): obj必须是type的一个实例或子类型

python
Arefe
Arefe
发布于 2017-05-03
9 个回答
Oğuz Şerbetci
Oğuz Şerbetci
发布于 2022-08-02
已采纳
0 人赞同

另一种发生这种错误的方式是,当你用朱庇特笔记本中的类重新加载模块时。

简单的解决办法是重新启动内核。

http://thomas-cokelaer.info/blog/2011/09/382/

Check out @Mike W's 答案 以了解更多细节。

Trips me up every time. :)
我也以为我疯了,但这就是我一直得到这个错误的原因。 简单地退出并重新进入Python,就解决了这个问题。 谢谢!
每年都要读一次,就像我的蟒蛇圣诞节。有什么办法能永远解决这个问题吗?
Moses Koledoye
Moses Koledoye
发布于 2022-08-02
0 人赞同

你应该使用 super 类作为第一个参数来调用 UrlManager ,而不是使用 URL 模型。替换代码0】不能用一个 不相关的 类别/类型。

From the docs,

super(type[, object-or-type]) : 返回一个代理对象,该对象将方法调用委托给一个父类或 类型的父类或同级类。

So you 不能 do:

>>> class D:
...    pass
>>> class C:
...    def __init__(self):
...        super(D, self).__init__()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in __init__
TypeError: super(type, obj): obj must be an instance or subtype of type

You should do:

qs_main = super(UrlManager, self).all(*args, **kwargs)

Or in Python 3:

qs_main = super().all(*args, **kwargs)
    
追踪结果还是一样吗?你展示的那个显示错误来自管理器的 all 方法,我在答案中已经解决了这个问题。
最近没怎么用django,但我想知道,这里有理由使用旧的风格化的类吗?
另外,我认为super对旧式风格的班级不起作用,是吗?
super不应该以父类为第一参数被调用。
Mike W
Mike W
发布于 2022-08-02
0 人赞同

在@Oğuz Şerbetci的回答中,在python3中(不仅仅是在Jupyter中),当需要重新加载一个库时,例如我们有 class Parent class Child 定义为

class Parent(object):
    def __init__(self):
        # do something
class Child(Parent):
    def __init__(self):
        super(Child, self).__init__(self)

then if you do this

import library.Child
reload(library)
Child()

你会得到TypeError: super(type, obj): obj must be an instance or subtype of type,解决办法是在重载后重新导入类。

import library.Child
reload(library)
import library.Child
Child()
    
你好,请问这如何与自动加载魔法一起工作? %reload_ext autoreload , %autoreload 2 .
pymen
pymen
发布于 2022-08-02
0 人赞同

For Jupyter only 你可以把他的问题弄进来,因为 reload 的逻辑有一些错误( issue )

以下是一个简单的解决方案/解决方法,在问题没有得到解决之前,对我来说是有效的

  • Add typo like 1001xx at the bottom of the file which you call in the cell
  • Run your cell - you will see some exception, just skip it
  • Remove typo which was added on step 1
  • Run the cell
  • Profit
  • Eldamir
    Eldamir
    发布于 2022-08-02
    0 人赞同

    另一种有趣的方式是,如果一个分支的合并重复了这个类,那么在文件中你有两个同名的定义,例如

    class A(Foo):
        def __init__(self):
            super(A, self).__init__()
    class A(Foo):
        def __init__(self):
            super(A, self).__init__()
    

    如果你试图从对A的第一个定义的静态引用中创建一个实例,一旦它试图调用super。在__init__方法里面,A将引用A的第二个定义,因为它已经被覆盖了。解决办法当然是删除类的重复定义,这样它就不会被覆盖了。

    这似乎是永远不会发生的事情,但它就发生在我身上,当时我对两个分支的合并没有给予足够的关注。我的测试失败了,出现了问题中描述的错误信息,所以我想我应该把我的发现留在这里,尽管它并没有完全回答这个具体的问题。

    Robin maltaros
    Robin maltaros
    发布于 2022-08-02
    0 人赞同

    对于这个问题,我发现最好的解决办法是只用python 3。这样你就不需要指定 "super "的参数,那么你就不会再出现像这样写类的错误了。

    class D:
    class C(D):
        def __init__(self):
            super().__init__()# no arguments given to super()
        
    对我来说,我没有做 init on the super() when I did init 在我的课上。
    apeinsuit
    apeinsuit
    发布于 2022-08-02
    0 人赞同

    当你没有实例化子类时,这个错误也会跳出来。 而试图调用一个类本身的方法时,也会弹出错误,比如在.NET中。

    class Parent:
        def method():
    class Child(Parent):
        def method():
            super().method()
    P = Parent()
    C = Child
    C.method()
        
    谢谢。如果你使用的是一个没有任何成员变量的类,这就特别棘手了。例如,所有的东西都是at-staticmethod或at-classmethod解码器。那么你可能会倾向于直接调用MyInstance.mymethod而不是MyInstance().mymethod(注意大括号)
    marduk
    marduk
    发布于 2022-08-02
    0 人赞同

    与@Eldamir类似,我的解决方法是意识到我写了两个同名的类,而第二个类正在覆盖第一个类。

    如果是这种情况,请改变其中一个班级的名称。

    AnonymousUser
    AnonymousUser
    发布于 2022-08-02
    0 人赞同

    因此,我只是把一个表格粘贴在 forms.py .

    我只是快速看了一眼,看看是否需要改变什么,但我没有看到。

    Then I got this super(type, obj): obj必须是type的一个实例或子类型。 错误,所以我在浏览器上搜索了一下,但在我检查任何答案之前,我又看了一次,这次我发现了问题。

    正如你所看到的,这个问题的许多答案都说是 super 的问题。是的,我也遇到了同样的问题。

    请确保你看看你是否有任何 super ,并看看所添加的类是否与该类相符。至少我就是这么做的。

    之前和之后

    Where I spotted it in my code

    forms.py

    Before:

    class userProfileForm(ModelForm):
        class Meta:
            model = user_profile
            fields = ("user", "rating", )
        def __init__(self, *args, **kwargs):
            # https://stackoverflow.com/a/6866387/15188026
            hide_condition = kwargs.pop('hide_condition',None)
            super(ProfileForm, self).__init__(*args, **kwargs)
            if hide_condition:
                self.fields['user'].widget = HiddenInput()
    
    class userProfileForm(ModelForm):
        class Meta:
            model = user_profile
            fields = ("user", "rating", )
        def __init__(self, *args, **kwargs):
            # https://stackoverflow.com/a/6866387/15188026
            hide_condition = kwargs.pop('hide_condition',None)
            super(userProfileForm, self).__init__(*args, **kwargs)
            if hide_condition: