【转】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

原文地址:https://www.cnblogs.com/hester/p/11311208.html

时间: 2024-08-04 01:23:50

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

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

在执行python manage.py makemigrations时报错: TypeError: __init__() missing 1 required positional argument: 'on_delete' 解决方法: 在连接外键时加上: on_delete=models.CASCADE 执行python manage.py makemigrations时报错TypeError: __init__() missing 1 required positional argument

python框架Scrapy报错TypeError: 'float' object is not iterable解决

原因是:Twisted版本高了. 解决办法: 只要把Twisted库降级到16.6.0即可: 1 pip3 install Twisted==16.6.0 2 3 注:Twisted16.6.0安装后,会自动卸载高版本的Twisted python框架Scrapy报错TypeError: 'float' object is not iterable解决

python 报错TypeError: 'range' object does not support item assignment,解决方法

贴问题 nums = range(5)#range is a built-in function that creates a list of integers print(nums)#prints "[0,1,2,3,4]" print(nums[2:4])#Get a slice from index 2 to 4 (exclusive); prints '[2,3]" print(nums[2:])#Get a slice from index 2 to the end

ubuntu上跑python连接pg,报错 ImportError: No module named psycopg2

ubuntu上跑python连接pg,报错  ImportError: No module named psycopg2 [email protected]:~# python /home/zxw/PGWriterTest_m.py Traceback (most recent call last): File "/home/zxw/PGWriterTest_m.py", line 4, in <module> import psycopg2 ImportError: No

Python virtualenv安装库报错SSL: CERTIFICATE_VERIFY_FAILED

Python virtualenv安装库报错SSL: CERTIFICATE_VERIFY_FAILED 问题描写叙述 使用pip依照virtualenv报错,例如以下: pip install virtualenv Collecting virtualenv /opt/python27/lib/python2.7/site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:315: SNIMissingWarning: An

C# 调用win32 DLL报错 System.BadImageFormatException

C# 调用win32 DLL报错  System.BadImageFormatException 项目右键属性->项目设计器->生成->平台->把'默认设置(任何 CPU)'改为x86. 因为'任何 CPU'的程序在64位的机器上就会用运行为64位,而64程序是不能加载32位dll的

调用系统命令 system-config-kickstart 报错,解决办法如下

[[email protected] ~]# system-config-kickstart Xlib:  extension "RANDR" missing on display "localhost:10.0". /usr/share/system-config-kickstart/kickstartGui.py:103: GtkWarning: GtkSpinButton: setting an adjustment with non-zero page si

PHP多次调用Mysql存储过程报错解决办法

PHP多次调用Mysql数据库的存储过程会出现问题,主要问题为存储过程中执行多次SQL语句不能一一释放导致的,网上找了一些解决办法,比如使用 multi_query 然后一个一个释放,但是发现根本不适合我们的项目,我们使用CI框架写的,更多的是使用CI的数据库处理方法.所以只能另辟蹊径. 一次偶然,把Mysql链接方式改成了mysqli,两种不同的PHP连接mysql的扩展,官方在高版本中推荐使用mysqli,结果却奇迹般好了,使用Mysql长连接也行,天意么? PHP多次调用Mysql存储过程

webdriver调用ie浏览器报错

webdriver调用ie浏览器报错: org.openqa.selenium.NoSuchWindowException: Unable to find element on closed 解决方法: System.setProperty("webdriver.ie.driver", "C:\\Users\\ssatyanarayana\\Downloads\\IEDriverServer.exe"); // opening the IE with recomen