我有一堂课如下:
class Hand():
def __init__(self, hand_a, play_deck, split_count, name): # hand_a for hand actual
self.hand_a = hand_a # the actual hand when instance created
self.play_deck = play_deck # need to move this to deck class
self.split_count = split_count
self.name = name
在另一个类中,我创建了一个 Hand 的实例:
class DECK():
def __init__(self):
pass
def deal(self, play_deck):
dhand = {}
phand = {}
for i in range (2):
play_deck, phand[i] = pick_item(play_deck)
play_deck, dhand[i] = pick_item(play_deck)
# creat instance of Hand for player's starting hand
self.start_hand = Hand(phand, play_deck, 0, "Player 1")
在第三堂课中,我试图访问我的第一个 Hand 实例,称为“start_hand”:
class Game():
def __init__(self):
pass
def play_game(self):
self.deck = DECK()
self.deck.deal(play_deck)
print "dhand = %r" % start_hand.hand_a
但我收到以下错误:
print "dhand = %r" % start_hand.hand_a
NameError: global name 'start_hand' is not defined
我也试过:
print "dhand = %r" % self.start_hand.hand_a
但我收到以下错误:
print "dhand = %r" % self.start_hand.hand_a
AttributeError: Game instance has no attribute 'start_hand'
我是否必须以其他方式创建类实例,还是必须以不同的方式访问它或两者兼而有之?还是我离得太远了,我应该重新开始?