[FRPythoneers] Object instantiation (newbie question)

DeVerter at robinsonmechanical.com DeVerter at robinsonmechanical.com
Wed Aug 1 15:40:52 MDT 2001


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.






More information about the FRPythoneers mailing list