第四十讲<拾遗>为什么我照着小甲鱼写的报错了?
这是我的class Turtle:
def _init_(self,x):
self.num=x
class Fish:
def _init_(self,x):
self.num=x
class Pool:
def _init_(self,x,y):
self.turtle=Turtle(x)
self.fish=Fish(y)
def print_num(self):
print('水池里共有乌龟%d只,小鱼%d条!'%(self.turtle.num,self.fish.num)
调用:
>>> pool=Pool(1,10)
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
pool=Pool(1,10)
TypeError: Pool() takes no arguments
>>>
小甲鱼的
经典的下划线问题
魔法方法都是两个下划线,你的代码只有一个
附源码
class Turtle:
def __init__(self,x):
self.num=x
class Fish:
def __init__(self,x):
self.num=x
class Pool:
def __init__(self,x,y):
self.turtle=Turtle(x)
self.fish=Fish(y)
def print_num(self):
print('水池里共有乌龟%d只,小鱼%d条!'%(self.turtle.num,self.fish.num))
是哦,谢谢哈
页:
[1]