super(type, obj): obj must be an instance or subtype of type

前端 未结 5 1303
天涯浪人
天涯浪人 2020-12-11 00:34

I work on a small Django app and get an error tells me, super(type, obj): obj must be an instance or subtype of type. I get it from the view

5条回答
  •  鱼传尺愫
    2020-12-11 01:05

    Another interesting way is if a merge of branches has duplicated the class, so that in the file you have two definitions for the same name, e.g.

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

    If you try to create an instance from a static reference to the first definition of A, once it tries to call super, inside the __init__ method, A will refer to the second definition of A, since it has been overwritten. The solution - ofcourse - is to remove the duplicate definition of the class, so it doesn't get overwritten.

    This may seem like something that would never happen, but it just happened to me, when I wasn't paying close enough attention to the merge of two branches. My tests failed with the error message described in the question, so I thought I'd leave my findings here, even though it doesn't exactly answer the specific question.

提交回复
热议问题