【发布时间】:2013-04-13 10:17:07
【问题描述】:
class Cat(object):
def __init__(self,run,fast):
self.run=run
self.fast=fast
def skip(self,to,store):
return 'Cat'
class Dog(Cat):
def __init_(self,run, fast,tongue):
Cat.__init__(self,run,fast)
self.tongue=tongue
def skip(self,to,store,happier):
return 'Doggy'
class Parent(object):
def __init__(self,velocity):
self.velocity=velocity
"""
velocity is a list of speeds or something. But it can
be from dogs or cats. Fast is a single number
"""
class Owner(Parent):
def __init__(self,smile):
Parent.__init__(self,velocity)
self.smile=smile
self.jumper=[]
def update(self):
# velocity is a list of instances of cats and dog
for number in self.velocity:
if isinstance(number,Dog):
number.skip(to,store,happier)
self.jumper.append(number)
if isinstance(number,Cat):
number.skip(to,store)
self.jumper.append(number)
不断出现的问题是,如果我在 if 实例更新方法中转到第一个 if 语句,它一直给我一个类型错误,它说我只需要为 skip 而不是三个参数。但是我知道因为它在块中,所以我必须拥有的实例是 dog,它需要三个参数。为什么会一直出现这种类型的错误?
【问题讨论】:
-
等等,你在运行中输入什么?
-
运行可能是这样的:
-
您可能会遇到错误,因为 Owner 没有以 run 开头的属性
-
另外,你的堆栈跟踪是什么?看起来,你应该得到一个 AttributeError
标签: python class error-handling