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)class son(father):def __init__(self,age):super().__init__(age)self.toy_number = 5def get_toy_number(self):print(self.toy_number)myson = son(6)
myson.get_age()
myson.get_toy_number()
运行时报错:“TypeError: super() takes at least 1 argument(0 given)”
原因:
上述继承方法super()的调用是python3中的方法,而运行环境是在python2中进行的,所以会存在不兼容的情况出现。
如果要在python2的环境中运行,那么需要将参考《python编程:从入门到实践》:
super().__init__(age)
改为:
super(son,self).__init__(age)
改完以后,运行时报错:
“TypeError: super() argument 1 must be type, not classobj”
上述方法行不通,需要进行以下的更改:
方法一:
class father(object): #这里添加objectdef __init__(self,age):self.age = agedef get_age(self):print(self.age)class son(father):def __init__(self,age):super(son, self).__init__(age) #这里改为super(son,self).__init__(age)self.toy_number = 5def get_toy_number(self):print(self.toy_number)myson = son(6)
myson.get_age()
myson.get_toy_number()
方法二:
class father():def __init__(self,age):self.age = agedef get_age(self):print(self.age)
class son(father):def __init__(self,age):father.__init__(self,age) #此处的参数含selfself.toy_number = 5def get_toy_number(self):print(self.toy_number)
myson = son(6)
myson.get_age()
myson.get_toy_number()
参考
python 函数继承调用super()初始化报错“TypeError: super() takes at least 1 argument”相关推荐
- 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 ...
- django报错TypeError: __init__() missing 1 required positional argument: 'on_delete'
个人博客页面链接:http://www.shihao.online (django搭建的个人博客,还在完善中) django报错 TypeError: __init__() missing 1 req ...
- 解决报错TypeError: people() takes no arguments
在学习构造方法的过程中遇到报错TypeError: people() takes no arguments 看似没有问题,但是就是运行不出来, 原因居然是:构造函数 def __init__(self ...
- python创建对象后调用对象的方法,报错TypeError: getName() takes 0 positional arguments but 1 was given
源码 ## TODO: Create multiple cars and visualize them height = 4 width = 6 world = np.zeros((height, w ...
- Python报错 TypeError: super(type, obj): obj must be an instance or subtype of type
今天在Spyder里用BeautifulSoup的时候跑代码报这个TypeError. 遇到这个问题之后在网上搜了一下也不知道怎么解决,结果在Spyder里重启Ipython内核之后,就没有报错能顺利 ...
- Python 3 报错 TypeError: object() takes no parameters【新手必过坑之一】
在学习 Python 3 类的时候,有如下程序: class Dog():def __init__(self,name,age):#初始化属性name和ageself.name = nameself. ...
- Python类传参报错-TypeError:takes 2 positional arguments but 3 were given
Error: 今天写了个类,通过requests传入url和headers来获取接口返回的数据,函数写好了但是出现了报错问题: TypeError:takes 2 positional argumen ...
- python二维数组读取数报错TypeError: list indices must be integers or slices, not tuple
如果直接使用如下方法构造二维数据: test_list = [[0] * 5 for _ in range(5)] 取数时报错TypeError: list indices must be integ ...
- PyCharm 添加 外键的时候 报错 TypeError:__init__()missing 1 required positional argument:‘on_delete‘
主要是 Django 版本问题,由2.0 后就需要添加上这一块的信息. 在对应的对象 外键 ,例如: class Comment(models.Model):content = models.Text ...
最新文章
- 从尾到头打印单向链表
- ds1302模块 树莓派_(16)给树莓派B+ 安装一个实时时钟芯片DS1302
- ITK:将样条曲线拟合到点集
- for循环批量写文件 shell_shell脚本:for循环批量重命名带空格文件名的文件
- C#操作存储过程,输入参数,返回结果
- NUC1742 Subsequence【前缀和+二分搜索+尺取法】
- python 西门子触摸屏通讯_PLC通过以太网与触摸屏通讯
- 矢量网络分析仪--测天线时使用技巧
- Latex常用数学公式整理——导数
- 亚马逊要验证收款查关联?
- DNS 的工作原理——域名系统
- Python根据地名获取经纬度
- 怎么把Word翻译成中文?建议收藏这些方法
- java spring+mybatis整合实现爬虫之《今日头条》搞笑动态图片爬取
- RoboCup仿真3D底层通信模块介绍(二)
- mysql移植海思_live555 交叉编译移植到海思开发板
- 手把手教你玩转谷歌TensorFlow
- unity3d音频系统
- 10 款牛哄哄的 Chrome 插件
- 2022-04-26 linux文件系统解读