TypeError: super(type, obj): obj must be an instance or subtype of type
问题
今天学习《Python Web 开发实战》自定义转换器这一小节,书中有段代码如下:
class ListConverter(BaseConverter):def __init__(self, url_map, separator="+"):super(ListConverter, self).__init__(url_map)self.separator = urllib.parse.unquote(separator)def to_python(self, value):return value.split(self.separator)def to_url(self, values):return self.separator.join(super(ListConverter, self).to_url(value)for value in values)
倒没什么问题,是可以正常运行的。
然而我对 super()
的用法不是很满意,毕竟在 Python3 环境下开发,super(ListConverter, self) 有硬编码嫌疑。于是代码修改如下:
class ListConverter(BaseConverter):def __init__(self, url_map, separator="+"):super().__init__(url_map)self.separator = urllib.parse.unquote(separator)def to_python(self, value):return value.split(self.separator)def to_url(self, values):return self.separator.join(super().to_url(value)for value in values)
结果一运行,解释器报错如下:
TypeError: super(type, obj): obj must be an instance or subtype of type
原因
经过多次尝试,发现问题出现在 self.separator.join(super().to_url(value) for value in values) 这段代码里。于是我做了个测试。
class A(object):def print_what(self, what):print(what)class B(A):def print_what(self, what):[super() for __ in range(5)]if __name__ == "__main__":b = B()b.print_what("hello")------------------------------------
TypeError: super(type, obj): obj must be an instance or subtype of type
果然异常,当我将 super() 改为 super(B, self)
又一切正常。
是因为 super() 在一个成员函数中多次调用造成的吗?那就不用列表解析式,换寻常的循环语句:
class B(A):def print_what(self, what):for __ in range(5):super()
结果运行正常。
事实上,在成员函数中使用 super() 时,Python 会自动传参,我猜测是解析式影响了传进去的参数。但遗憾的是,我不知道该如何调试,才能获得错误情况下,传进去的参数是什么。
解决方案
针对此次遇到的问题,解决方法有以下三种。
第一种
使用 super(ListConverter, self)
代替 super()
第二种
不用解析式,用寻常的 for 循环。
class ListConverter(BaseConverter):...def to_url(self, values):tmplist = []for value in values:tmplist.append(super().to_url(value))return self.separator.join(tmplist)
第三种
使用解析式,但不在解析式中调用 super()。
class ListConverter(BaseConverter):...def to_url(self, values):sup = super()return self.separator.join(sup.to_url(value)for value in values)
TypeError: super(type, obj): obj must be an instance or subtype of type相关推荐
- TypeError: super(type, obj): obj must be an instance or subtype of type这个问题怎么处理?
TypeError: super(type, obj): obj must be an instance or subtype of type这个问题怎么处理? 这个错误通常发生在使用super()函 ...
- Python提示 TypeError: super(type, obj): obj must be an instance or subtype of type问题
Python提示 TypeError: super(type, obj): obj must be an instance or subtype of type问题 简述问题 在工作中有一天将debu ...
- TypeError: super(type, obj): obj must be an instance or subtype of type 该错误的一次处理
我在写maya类的时候遇到,版本python2.7 folder lib mayaclass.py base类 A(base)类 ui.py 其中A继承base ui.py文件中内容 #python2 ...
- 成功解决TypeError: super(type, obj): obj must be an instance or subtype of type
super(Graphnet, self).init()改为 super().init()
- Python报错 TypeError: super(type, obj): obj must be an instance or subtype of type
今天在Spyder里用BeautifulSoup的时候跑代码报这个TypeError. 遇到这个问题之后在网上搜了一下也不知道怎么解决,结果在Spyder里重启Ipython内核之后,就没有报错能顺利 ...
- Flutter type ‘Future<bool>‘ is not a subtype of type ‘bool‘ in type cast
这个问题是我在使用flutter shared_preferences的时候,自己默认写了几个存储和读取的字段函数,然后出错了 问题在! 我使用的时候非常耿直 如图 getUserNameInput函 ...
- python 调用super()初始化报错“TypeError: super() takes at least 1 argument”
在python中有如下代码: class father():def __init__(self,age):self.age = age;def get_age(self):print(self.age ...
- python 函数继承调用super()初始化报错“TypeError: super() takes at least 1 argument”
在python中有如下代码: class father():def __init__(self,age):self.age = agedef get_age(self):print(self.age) ...
- #报错解决#TypeError Failed to execute ‘readAsText‘ on ‘FileReader‘ parameter 1 is not of type ‘Blob‘
问题描述 在实现需求:批量下载的时候,返回的数据类型设置为blob $http({method: 'post',url: dataurl,data: bodyParam,headers: { 'Con ...
最新文章
- 划水是程序员必备的技能吗? | 每日趣闻
- [9.28模拟] good
- python电脑下载有问题-Python 解决火狐浏览器不弹出下载框直接下载的问题
- 面试后总是没有结果的7大原因
- 李郁韬:短期爆发还是未来趋势?腾讯云海量音视频通信服务背后的技术发展
- C语言之去掉https链接的默认443端口
- 前端学习(2989):vue+element今日头条管理--项目结束
- java与python难度对比_Python和Java的区别,看完这篇文章你就清楚啦
- 数论 —— 高次同余方程与 BSGS 算法
- linux memcacheQ的安装与使用
- 250分b区计算机专硕,2021兰州大学研究生复试分数线
- python字典浅复制_元组,字典,浅复制,集合
- PIFA-平面倒F天线[搬运]
- matlab:xlsread
- 中国哲学书电子化计划
- 2020 Q4营收环比增长27.5%,前程无忧找回增长节奏
- Fiddler 和 Wireshark抓包教程合集
- BMapGL实现地图轨迹运动(地图视角不变)
- spss对数据进行因子分析
- react-native实现微信分享和微信支付(安卓端)