Refinement Re: [FRPythoneers] Object instantiation (newbie question)

Ken Kinder kkinder at tridog.com
Wed Aug 1 15:56:00 MDT 2001


I think my last email was a little unclear. Let me refine:

	All object and identifier names should be assigned by the programmer.
If an object is given a name by the user, it should be anonymous
(nameless) object stored in a data structure that relates the
user-supplied identification to the object itself.

	In short, what you did.

This reminds me of Zope and how it stores trees -- each "attribute" is
actually in a dictionary somewhere, not stored as an actual attribute. I
think.

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