当学习面向对象的类和实例的时候,我一开始输入书中的代码,结果出现了 () takes no parameters问题,过程如下:
class Student( ):
def _init_(self,name,score):
self.name = name
self.score = score
def print_score(self):
print \'%s: ,%s\' % (self.name,self.score)
>>> bart = Student(\'Bart Simpon\',49)
Traceback (most recent call last):
File \"<pyshell#16>\", line 1, in <module>
bart = Student(\'Bart Simpon\',49)
TypeError: () takes no parameters
原因通过搜索,已解决。问题是init前面和后面都应该有两天下划线,咋一看书上的程序,原以为是前后各有一条下划线,在原程序上在init两边再各添一条下划线即可:
class Student( ):
def __init__(self,name,score):
self.name = name
self.score = score
def print_score(self):
print \'%s: ,%s\' % (self.name,self.score)
>>> bart = Student(\'Bart Simpon\',49)
>>> bart.name
\'Bart Simpon\'
>>> bart.score
49
问题解决,其实上下对比一下,双下划线跟单下划线还是有一定区别的,但是如果不注意还是很容易看错。
继续阅读与本文标签相同的文章
上一篇 :
当前深度神经网络模型压缩和加速方法速览
-
万余平方米演示自动驾驶,世界智能网联汽车大会来了!
2026-05-19栏目: 教程
-
独家解读 etcd 3.4版本 |云原生生态周报 Vol. 18
2026-05-19栏目: 教程
-
5大高清免费无版权图片网站,设计、自媒体都不是问题,值得收藏
2026-05-19栏目: 教程
-
物联网平台实用技巧:设备端检测自己是否在线
2026-05-19栏目: 教程
-
阿里云代金券+9折优惠码实践,原价2381元的云服务器实际购买价1943元
2026-05-19栏目: 教程
