[英]Python - TypeError: super(type, obj): obj must be an instance or subtype of type?
[英]super(type, obj): obj must be an instance or subtype of type
為什么會出現以下錯誤,如何解決?
TypeError: super(type, obj): obj 必須是類型的實例或子類型
發生此錯誤的另一種方式是在 Jupiter 筆記本中使用類重新加載模塊時。
簡單的解決方案是重新啟動內核。
http://thomas-cokelaer.info/blog/2011/09/382/
查看@Mike W 的答案以獲取更多詳細信息。
您應該使用UrlManager
類作為第一個參數而不是URL
模型來調用super
。 super
不能與不相關的類/類型一起調用:
從文檔中,
super(type[, object-or-type])
:返回一個代理對象,該對象將方法調用委托給類型的父類或兄弟類。
所以你不能這樣做:
>>> class D:
... pass
...
>>> class C:
... def __init__(self):
... super(D, self).__init__()
...
>>> C()
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
你應該做:
qs_main = super(UrlManager, self).all(*args, **kwargs)
或者在 Python 3 中:
qs_main = super().all(*args, **kwargs)
在@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)
那么如果你這樣做
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()
僅適用於 Jupyter您可以解決他的問題,因為reload
邏輯有一些錯誤(問題)
這是一個簡單的解決方案/解決方法,在問題未解決之前對我有用
1001xx
之類的錯字另一個有趣的方式是如果分支的合並復制了類,那么在文件中你有兩個同名的定義,例如
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
,因為它已被覆蓋。 解決方案——當然——是刪除類的重復定義,這樣它就不會被覆蓋。
這似乎是永遠不會發生的事情,但它只是發生在我身上,當時我沒有足夠關注兩個分支的合並。 我的測試因問題中描述的錯誤消息而失敗,所以我想我會在這里留下我的發現,即使它沒有完全回答特定問題。
我為這個問題找到的最好的解決方案只能使用 python 3。然后你不需要指定“super”的參數,那么你就不會再像這樣編寫你的類了:
class D:
pass
class C(D):
def __init__(self):
super().__init__()# no arguments given to super()
所以我只是在forms.py中粘貼了一個表格。
我只是快速瀏覽了一下是否需要更改任何內容,但我沒有看到。
然后我得到了這個super(type, obj): obj must be an instance or subtype of type錯誤,所以我在瀏覽器上搜索它,但在我檢查任何答案之前,我又看了一次,這次我發現了問題。
如您所見,這個問題的許多答案都說super
有問題。 是的,這對我來說是同樣的問題。
確保您查看是否有任何super
,並查看添加的 class 是否與 class 匹配。 至少我是這樣做的。
之前和之后
我在代碼中發現它的地方
forms.py
前:
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:
self.fields['user'].widget = HiddenInput()
您會看到 super 已更改為 class 名稱
當您只是不實例化子類並嘗試調用類本身的方法時,也會出現此錯誤,例如:
class Parent:
def method():
pass
class Child(Parent):
def method():
super().method()
P = Parent()
C = Child
C.method()
與@Eldamir 類似,我意識到我已經編寫了兩個同名的類,而第二個類覆蓋了第一個類,從而解決了這個問題。
如果是這種情況,請更改其中一個類的名稱。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.