[FRPythoneers] Object instantiation (newbie question)

Rob Riggs rob at pangalactic.org
Wed Aug 1 18:46:51 MDT 2001


I would envision something along the lines of:

class Game:

    def __init__(self):
        self._dealer = Dealer()
        self._players = []

    def addPlayer(self):
        name = getName()
        self._players.append(Player(name))

class Dealer:
    """Dealer shuffles deck and deals cards to Players"""
    pass

class Player:

    def __init__(self, name):
        self._name = name
        self._hand = Hand()
    def deal(self, card):
        self._hand.add(card)

class Card: pass

class Hand: pass

class Deck: pass


Your game is encapsulated by the Game class, which owns a list of 
players. This uses all of the bits of advice that Ken and Sean gave out. 
It also adds some more pieces to the puzzle, adding in all of the object 
you're likely to find at a casino blackjack table (save the chips and 
drinks).


DeVerter at robinsonmechanical.com wrote:

>Hi,
>     I am not sure how to handle the objects I am creating in my first
>'real' Python script - a little black-jack program.  At this point, I am
>instantiating at least two objects, Hand and Card; a Hand() would contain
>Cards().  As you can see from the script below I do achieve creating the
>Hand() objects but they are being stored in the dictionary, player_dict{}.
>This works because I can refer to them by their key but I wonder if it
>would be 'better' to instantiate these objects outside of the list and the
>object name would be the name from get_name().
>
>In summary:
>1)  How do I create the objects away from the dictionary using the name
>entered as the object name?
>2)  Which is better, leaving the objects in the dictionary or instantiating
>them 'in the open'?
>
>Thanks for your suggestions,
>Mark d.
>
>------------
>class Hand:
>     def __init__(self, name):
>          self.cards=[]
>          self.name=name
>
>class Card:
>     pass
>     # withheld code since its not necessary here.
>
>def get_name():
>     name=raw_input("Enter name for player %s: " % (player+1))
>     return name
>
>num_players=input("How many players? ")
>player_dict={}
>
>for player in range(num_players):
>     player_dict[player]=get_name()
>     player_dict[player]=Hand(player_dict[player])
>
>-----------------
>
>BTW:  Acknowledgements to 'How To Think Like A Computer Scientist, Python
>Edition' for the Deck(), Hand() and Card() classes I am using in my silly
>little program.
>
>
>
>_______________________________________________
>This message sent by the FRPythoneers mailing list.
>Unsubscribe: echo unsubscribe | FRPythoneers-request at lists.community.tummy.com
>URL: http://lists.community.tummy.com/mailman/listinfo/frpythoneers
>






More information about the FRPythoneers mailing list