You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python中super()函数参数错误:需传入type而非classobj,求技术协助

解决super()调用时报"argument 1 must be type, not classobj"错误

嘿,我之前也碰到过这个问题!这个错误的核心原因是你的父类Preprocessor属于Python旧式类(也就是没有继承object的类),而super()函数只支持新式类(继承自object的类)。

先看你的代码片段:子类MsrpCorpusPreprocessor继承了Preprocessor,但如果Preprocessor是像下面这样定义的旧式类:

class Preprocessor:
    # 父类内部逻辑

而不是新式类的写法:

class Preprocessor(object):
    # 父类内部逻辑

就会触发这个报错。

给你两种可行的解决办法:

办法一:把父类改为新式类

如果有修改Preprocessor定义的权限,直接让它继承object即可,这样super()就能正常工作了:

class Preprocessor(object):
    def __init__(self, embed_file, max_document_length, unknown, pad):
        # 父类初始化逻辑
        pass

办法二:直接调用父类的构造函数

要是没办法修改父类(比如它来自第三方库),可以绕过super(),直接调用父类的__init__方法:

class MsrpCorpusPreprocessor(Preprocessor): 
    def __init__(self, embed_file): 
        Preprocessor.__init__(self, 
            embed_file=embed_file, 
            max_document_length=48, 
            unknown="*UNKNOWN*", 
            pad="*PAD*"
        )

这种写法不依赖新式类,在旧式类场景下能稳定运行,兼容性拉满。

另外补充个小细节:如果是在Python3环境中,所有类默认都是新式类,但如果你的Preprocessor是用Python2风格写的旧式类(没继承object),还是会出现这个问题,这时候用办法二就能快速解决。

内容的提问来源于stack exchange,提问作者susane

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 06:38:45