一、实验环境

1.Windows7x64_SP1

2.Anaconda2.5.0 + python2.7(anaconda集成,不需单独安装)

二、实验步骤

2.1 在python中有如下代码:

class father():
  def __init__(self,age):
    self.age = age;
  def get_age(self):
    print(self.age); class son(father):
  def __init__(self,age):
    super().__init__(age);
    self.toy_number = 5;
  def 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)”

2.2 原因分析

该方法调用super()为在python3中的方法,而此是在python2中运行的,在python3中运行将正常。

在《python编程:从入门到实践》一书中介绍了若想在python2中运行需将

super().__init__(age)

一句改为:

super(son, self).__init__(age)

但我按此方法改后,运行时报错:“TypeError: super() argument 1 must be type, not classobj”

2.3 解决方式

上网查询资料后,得知若想要在python2中运行成功,可以改为如下两种方法:

方法一

class father(object):
  def __init__(self,age):
    self.age = age;
  def get_age(self):
    print(self.age); class son(father):
  def __init__(self,age):
    super(son, self).__init__(age);
    self.toy_number = 5;
  def 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 = age;
  def get_age(self):
    print(self.age); class son(father):
  def __init__(self,age):
    father.__init__(self,age);#注意此处参数含self
    self.toy_number = 5;
  def get_toy_number(self):
    print(self.toy_number); myson = son(6)
myson.get_age()
myson.get_toy_number()

运行后都将得到正确答案:

参考链接:https://stackoverflow.com/questions/9698614/super-raises-typeerror-must-be-type-not-classobj-for-new-style-class
原文请参考:https://blog.csdn.net/u010812071/article/details/76038833

【转】python 调用super()初始化报错“TypeError: super() takes at least 1 argument”的更多相关文章

  1. Django :执行 python manage.py makemigrations 时报错 TypeError: __init__() missing 1 required positional argument: 'on_delete'

    原因 执行命令 python manage.py makemigrations 报错 TypeError: __init__() missing 1 required positional argum ...

  2. 关于Jupyter Notebook无法自动补全(Autocompletion),报错TypeError: __init__() got an unexpected keyword argument 'column' 的解决方案

    关于Jupyter Notebook无法自动补全(Autocompletion),报错TypeError: __init__() got an unexpected keyword argument ...

  3. python报错 TypeError: a() got multiple values for argument 'name'

    [问题现象] 在一次调用修饰函数中出现了问题,折腾了一下午,一直报错 TypeError:  got multiple values for argument 只是很简单的调用 from tsu2Ru ...

  4. python2.7 使用super关键词 报错 TypeError: must be type, not&n

    错误试验代码: class Base(): def meth(self): print "i'm base" class Derived(Base): def meth(self) ...

  5. 执行python manage.py makemigrations时报错TypeError: __init__() missing 1 required positional argument: 'on_delete'

    在执行python manage.py makemigrations时报错: TypeError: __init__() missing 1 required positional argument: ...

  6. python3.7的celery报错TypeError: wrap_socket() got an unexpected keyword argument '_context'

    原启动方法为: 起执行任务的服务 elery worker -A celery_task -l info -P eventlet 起提交任务的服务 celery beat -A celery_task ...

  7. Django 生成数据库表时的报错TypeError: __init__() missing 1 required positional argument: 'on_delete'

    原因及解决办法: https://www.cnblogs.com/phyger/p/8035253.html

  8. Python中常见的报错名称

    Python中常见的报错名称 1.SyntaxError 语法错误.看看是否用Python关键字命名变量,有没有使用中文符号,运算符.逻辑运算符等符号是不是使用不规范. 2.IndentationEr ...

  9. appium 提示报错“TypeError: 'unicode' object is not callable”的解决方式!

    这里提到的这个报错,是小错误且容易经常会犯,有时需要特别注意使用. 目的要求结果:根据某个元素的id值获取到对应id的text值,并且将获取的text值与本身存在的text值做比较,查看text值是否 ...

随机推荐

  1. C# 方法的out、ref、params参数

    一.out参数实例 [实例]求一个数组中的最大值.最小值.总和.平均值 class Program { static void Main(string[] args) { //写一个方法 求一个数组中 ...

  2. Vertx和Jersey集成使用

    为了更好地解耦和提高性能,一般将工程的接口部分剥离出来形成一个单独的工程,这样不仅能提高性能,增强可维护性,并且在后台工程宕掉的话对客户端接口的影响较小. 公司使用了Vertx和Jersey,Vert ...

  3. 前端之:js

    JavaScript概述 ECMAScript和JavaScript的关系 1996年11月,JavaScript的创造者--Netscape公司,决定将JavaScript提交给国际标准化组织ECM ...

  4. https://www.cnblogs.com/kxm87/p/9268622.html

    数据库使用MySQL,ORM使用spring data jpa 1 因此需要再pom.xml文件中添加相应jar包.如下: <!-- 引入jap --> <dependency> ...

  5. FCC-学习笔记 Sorted Union

    FCC-学习笔记  Sorted Union 1>最近在学习和练习FCC的题目.这个真的比较的好,推荐给大家. 2>中文版的地址:https://www.freecodecamp.cn/; ...

  6. NBIOT实现UDP协议的发送和接收(包含软件升级)

    源码下载: nbiot_module程序(java netbean) -> 提取码 UdpServer程序(C# vs2010) -> 提取码 QQ:505645074 前提条件:开NB卡 ...

  7. Scrum冲刺第三篇

    一.每日例会 会议照片 成员 昨日已完成的工作 今日计划完成的工作 工作中遇到的困难 陈嘉欣 撰写博客,管理成员提交代码 每日博客,根据队员代码问题更改规范文档安排后续工作 队员提交的代码管理困难 邓 ...

  8. CVE-2019-0708漏洞利用复现

    CVE-2019-0708漏洞利用复现 距离这个漏洞爆出来也有好几个月了,之前一直忙也没来得及写文档,现在重新做一遍. 准备环境: win7靶机 IP地址:172.16.15.118 Kali攻击机 ...

  9. JDBC学习笔记二

    JDBC学习笔记二 4.execute()方法执行SQL语句 execute几乎可以执行任何SQL语句,当execute执行过SQL语句之后会返回一个布尔类型的值,代表是否返回了ResultSet对象 ...

  10. 69.x的平方根 (平)(简单)

    实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去. 示例 1: 输入: 4输出: 2示例 2: ...