class Card(object):
suitList = ("Clubs", "Diamonds", "Hearts", "Spades")
rankList = ("narf", "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King")
def __init__(self, suit = 0, rank = 2):
self.suit = suit
self.rank = rank
def __str__ (self):
return "%s of %s" % (self.rankList[self.rank], self.suitList[self.suit])
# override built-in compare function:
def __cmp__(self, other):
# use if instead of if...elif..else
if self.suit > other.suit: return 1
if self.suit < other.suit: return -1
if self.rank == 1 and other.rank != 1: return 1
if self.rank != 1 and other.rank == 1: return -1
if self.rank > other.rank: return 1
if self.rank < otehr.rank: return -1
return 0
# test code
card1 = Card(0, 12)
card2 = Card(1, 2)
print card1.cmp(card2)
以下はエラーメッセージです:トレースバック(最後の最後の呼び出し):ファイル "C:\ Python27 \ OOP_thecardclass.py"、29行目、print card1.cmp(card2)AttributeError:'Card'オブジェクトには属性'cmp'がありません
なんで?