python 调用super()初始化报错“TypeError: super() takes at least 1 argument”
来源:互联网 发布:网上商城源码 java 编辑:程序博客网 时间:2024/06/16 21:18
在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)”
原因是该方法调用super()为在python3中的方法,而此是在python2中运行的,在python3中运行将正常。
在《python编程:从入门到实践》一书中介绍了若想在python2中运行需将
super().__init__(age);
一句改为:
super(son, self).__init__(age);
但我按此方法改后,运行时报错:“TypeError: super() argument 1 must be type, not classobj”
上网查询资料后,得知若想要在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);#注意此处参数含selfself.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
阅读全文
0 0
- python 调用super()初始化报错“TypeError: super() takes at least 1 argument”
- python 调用super()初始化报错“TypeError: super() takes at least 1 argument”
- Python 调用super初始化报错 "super() argument 1 must be type, not classobj"
- Python 调用super初始化报错 "super() argument 1 must be type, not classobj"
- Python: TypeError: XXX() takes exactly 1 argument (2 given)
- TypeError: __init__() takes at least 4 arguments (4 given)
- TypeError: resize_images() takes at least 3 arguments (3 given)
- python报错 TypeError: object() takes no parameters
- PamIE 和COM调用IE出错: TypeError: getElementById() takes exactly 1 argument (2 given)
- super() fails with error: TypeError “argument 1 must be type, not classobj”
- PAMIE TypeError: getElementsByTagName() takes exactly 1 positional argument (2 given)
- TypeError: checkcode() takes exactly 1 argument (2 given)
- super init的初始化调用
- python2.7 使用super关键词 报错 TypeError: must be type, not classobj 解决办法
- Python 'takes exactly 1 argument (2 given)' Python error
- Python 'takes exactly 1 argument (2 given)' Python error
- python出现super.__init__:TypeError:must be type,not classobj
- django中报错TypeError: su() takes exactly 1 argument (2 given)的解决办法
- [leetcode]636. Exclusive Time of Functions
- CNTK API文档翻译(11)——使用LSTM预测时间序列数据(物联网数据)
- Effective C++笔记(8)—继承与面向对象设计
- Nginx服务器对session的处理策略
- 不要以多态的方式处理数组
- python 调用super()初始化报错“TypeError: super() takes at least 1 argument”
- va_list va_start va_arg va_end 使用方法
- 怎么上传自己的代码到github上
- java泛型
- [spring-boot] 集成shiro
- 刘汝佳--最长回文字符串
- JDBC连接Oracle、MySQL数据库
- 深度学习笔记(二)
- Myeclipse正则表达式替换代码