我是新来的蟒蛇,这是一类,我有平等操作符重载问题
class Goal:
def __init__(self, name, value):
self.name = name
self.value = value
def is_fulfilled(self):
return self.value == 0
def fulfill(self, value):
if(self.value < value):
value = self.value
self.value -= value
def debug(self):
print "-----"
print "#DEBUG# Goal Name: {0}".format(self.name)
print "#DEBUG# Goal Value: {0}".format(self.value)
print "-----"
def __eq__(self, other):
return self.name == other.name
当我做
if(goal1 == goal2):
print "match"
它提出了这个错误
File "/home/dave/Desktop/goal.py", line 24, in __eq__
return self.name == other.name
AttributeError: 'str' object has no attribute 'name'
什么我在这里做错了吗?
一个更好的办法是使用,而不是一个try-catch isinstance。 – 2015-07-16 20:48:27