在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”相关推荐

  1. 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 ...

  2. django报错TypeError: __init__() missing 1 required positional argument: 'on_delete'

    个人博客页面链接:http://www.shihao.online (django搭建的个人博客,还在完善中) django报错 TypeError: __init__() missing 1 req ...

  3. 解决报错TypeError: people() takes no arguments

    在学习构造方法的过程中遇到报错TypeError: people() takes no arguments 看似没有问题,但是就是运行不出来, 原因居然是:构造函数 def __init__(self ...

  4. 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 ...

  5. Python报错 TypeError: super(type, obj): obj must be an instance or subtype of type

    今天在Spyder里用BeautifulSoup的时候跑代码报这个TypeError. 遇到这个问题之后在网上搜了一下也不知道怎么解决,结果在Spyder里重启Ipython内核之后,就没有报错能顺利 ...

  6. Python 3 报错 TypeError: object() takes no parameters【新手必过坑之一】

    在学习 Python 3 类的时候,有如下程序: class Dog():def __init__(self,name,age):#初始化属性name和ageself.name = nameself. ...

  7. Python类传参报错-TypeError:takes 2 positional arguments but 3 were given

    Error: 今天写了个类,通过requests传入url和headers来获取接口返回的数据,函数写好了但是出现了报错问题: TypeError:takes 2 positional argumen ...

  8. 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 ...

  9. PyCharm 添加 外键的时候 报错 TypeError:__init__()missing 1 required positional argument:‘on_delete‘

    主要是 Django 版本问题,由2.0 后就需要添加上这一块的信息. 在对应的对象 外键 ,例如: class Comment(models.Model):content = models.Text ...

最新文章

  1. 从尾到头打印单向链表
  2. ds1302模块 树莓派_(16)给树莓派B+ 安装一个实时时钟芯片DS1302
  3. ITK:将样条曲线拟合到点集
  4. for循环批量写文件 shell_shell脚本:for循环批量重命名带空格文件名的文件
  5. C#操作存储过程,输入参数,返回结果
  6. NUC1742 Subsequence【前缀和+二分搜索+尺取法】
  7. python 西门子触摸屏通讯_PLC通过以太网与触摸屏通讯
  8. 矢量网络分析仪--测天线时使用技巧
  9. Latex常用数学公式整理——导数
  10. 亚马逊要验证收款查关联?
  11. DNS 的工作原理——域名系统
  12. Python根据地名获取经纬度
  13. 怎么把Word翻译成中文?建议收藏这些方法
  14. java spring+mybatis整合实现爬虫之《今日头条》搞笑动态图片爬取
  15. RoboCup仿真3D底层通信模块介绍(二)
  16. mysql移植海思_live555 交叉编译移植到海思开发板
  17. 手把手教你玩转谷歌TensorFlow
  18. unity3d音频系统
  19. 10 款牛哄哄的 Chrome 插件
  20. 2022-04-26 linux文件系统解读

热门文章

  1. 后缀数组——处理字符串的有力工具,好好学习
  2. mac电脑m1搭建java开发环境参考手册
  3. 2018十大航天事件
  4. 三星a9s参数_四摄镜头来了!三星Galaxy A9s参数曝光
  5. 90-0002 图鉴合成表
  6. 济民可信20亿战略资金助力大健康产业,一号护工建立护工行业标准
  7. 【五一节,请收下最诚挚的节日祝福】
  8. 阅面携手英特尔发布“繁星”的背后,是AI算法“端”和“云”之争
  9. 训练日记--数学基础
  10. 【DB2】 Instance Database DAS 基本管理