【转】python 调用super()初始化报错“TypeError: super() takes at least 1 argument”
一、实验环境
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”的更多相关文章
- 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 ...
- 关于Jupyter Notebook无法自动补全(Autocompletion),报错TypeError: __init__() got an unexpected keyword argument 'column' 的解决方案
关于Jupyter Notebook无法自动补全(Autocompletion),报错TypeError: __init__() got an unexpected keyword argument ...
- python报错 TypeError: a() got multiple values for argument 'name'
[问题现象] 在一次调用修饰函数中出现了问题,折腾了一下午,一直报错 TypeError: got multiple values for argument 只是很简单的调用 from tsu2Ru ...
- python2.7 使用super关键词 报错 TypeError: must be type, not&n
错误试验代码: class Base(): def meth(self): print "i'm base" class Derived(Base): def meth(self) ...
- 执行python manage.py makemigrations时报错TypeError: __init__() missing 1 required positional argument: 'on_delete'
在执行python manage.py makemigrations时报错: TypeError: __init__() missing 1 required positional argument: ...
- 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 ...
- Django 生成数据库表时的报错TypeError: __init__() missing 1 required positional argument: 'on_delete'
原因及解决办法: https://www.cnblogs.com/phyger/p/8035253.html
- Python中常见的报错名称
Python中常见的报错名称 1.SyntaxError 语法错误.看看是否用Python关键字命名变量,有没有使用中文符号,运算符.逻辑运算符等符号是不是使用不规范. 2.IndentationEr ...
- appium 提示报错“TypeError: 'unicode' object is not callable”的解决方式!
这里提到的这个报错,是小错误且容易经常会犯,有时需要特别注意使用. 目的要求结果:根据某个元素的id值获取到对应id的text值,并且将获取的text值与本身存在的text值做比较,查看text值是否 ...
随机推荐
- MySQL慢日志查询分析方法与工具
MySQL中的日志包括:错误日志.二进制日志.通用查询日志.慢查询日志等等.这里主要介绍下比较常用的两个功能:通用查询日志和慢查询日志. 1)通用查询日志:记录建立的客户端连接和执行的语句. 2)慢查 ...
- .net core 发布到iis问题 HTTP Error 500.30 - ANCM In-Process Start Failure
1. 没有在Program里配置IIS webBuilder.UseIIS(); 2. StartupProduction 里AutoFac容器注入错误和新版的CORS中间件已经阻止使用允许任意Ori ...
- HttpListener 实现小型web服务器
HttpListener 实现web服务器 用于小型服务器,简单.方便.不需要部署. 总共代码量不超过50行. static void Main(string[] args) { //创建HTTP监听 ...
- 剑指 Offer——3. 从尾到头打印链表
题目描述 输入一个链表,按链表值从尾到头的顺序返回一个ArrayList. 一般是不破坏链表结构 思路与实现 直接用栈存储就好了 public class Solution { public Arra ...
- Java自学-I/O 控制台输入流System.in
Java 控制台输入流 System.in和Scanner System.out 是常用的在控制台输出数据的 System.in 可以从控制台输入数据 步骤 1 : System.in package ...
- Python分页
# -*-coding:utf-8-*- # Author:Ds from django.utils.safestring import mark_safe from django.http.requ ...
- redis笔记3
redis持久化机制 redis提供了两种持久化策略 RDB RDB的持久化策略: 按照规则定时将内存的数据同步到磁盘 snapshot redis在指定的情况下会触发快照 自己配置的快照规则 sav ...
- http协议历史
HTTP 通常被译为超文本传输协议, 但这种译法并不严谨. 严谨的译名应该为“超文本转移协议”. 最初设想的基本理念是: 借助多文档之间相互关联形成的超文本( HyperText) , 连成可相互参阅 ...
- Flink使用SideOutPut替换Split实现分流
以前的数据分析项目(版本1.4.2),对从Kafka读取的原始数据流,调用split接口实现分流. 新项目决定使用Flink 1.7.2,使用split接口进行分流的时候,发现接口被标记为deprac ...
- 构建Apache Web服务器
Apache 是世界使用排名第一的 Web 服务器软件.它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的 Web 服务器端软件之一.Apache工作模式有多种,其 ...