TypeError: this constructor takes no arguments
来源:互联网 发布:零点咨询待遇 知乎 编辑:程序博客网 时间:2024/06/11 21:51
在使用python编写面向对象的程序时,若书写不小心很可能遇到TypeError: this constructor takes no arguments这个错误。
例如下面的程序:
class Student:def _init_(self,name,age,sex):self.name=nameself.age=ageself.sex=sexdef hello(self):print '初始化学生对象,姓名:%s,年龄%d,性别%s' %(self.name,self.age,self.sex)
运行报错如下:
出错原因是,在python中构造函数书写格式是__init__,而不是_init_,即在init两侧都是双下划线,不是单下划线。
因此原程序应改为:
class Student:def __init__(self,name,age,sex):self.name=nameself.age=ageself.sex=sexdef hello(self):print '初始化学生对象,姓名:%s,年龄%d,性别%s' %(self.name,self.age,self.sex)
此时在运行就正常了:
1 0
- TypeError: this constructor takes no arguments
- TypeError: this constructor takes no arguments
- TypeError: this constructor takes no arguments
- python之TypeError: this constructor takes no arguments
- python:TypeError: this constructor takes no arguments解决方法
- TypeError: this constructor takes no arguments(Python 学习遇到的错误1)
- type error:this constructor takes no arguments的错误提示(python)
- python编绎程序中出现this constructor takes no arguments
- python 使用装饰器提示:TypeError: wrapper() takes no arguments (1 given)
- Python 3.5 + PyInstaller 3.2.1打包exe出错,TypeError: read() takes no keyword arguments
- TypeError: object() takes no parameters
- TypeError: object() takes no parameters
- TypeError: object() takes no parameters
- TypeError: object() takes no parameters
- TypeError: object.__new__() takes no parameters
- TypeError: object.__init__() takes no parameters
- python "TypeError: object() takes no parameters"
- This fragment should provide a default constructor (a public constructor with no arguments
- Altium Designer 元件库元件名称及中英对照
- 学会爱
- 在eclipse环境下编译cmu sphinx4
- SWIFT的风险防范
- C++枚举类型
- TypeError: this constructor takes no arguments
- Install OpenCL on Debian, Ubuntu and Mint orderly
- cocos2d-x中模态对话框的一种实现方式
- Windows XP寿终正寝:我们真的恐慌吗?
- LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
- Spring中实现文件上传
- UDP 123
- Ubuntu下的绘图软件
- 微软困境:免费时代的落伍者