[Tutor] Methods that return instances of their own class?

Che M pine508 at hotmail.com
Thu Oct 15 19:33:51 CEST 2009

> In particular, I'm trying to run a simple prisoner's dilemma game, and  
> I want to make a "game" object that has a method which returns the  
> "game" object with the payoffs reversed; that is, the payoff matrix  
> from the other player's point of view.  Basically a kind of transpose  
> which is specific to this application.

Since this is the tutor list, I'd like to ask some questions about
structures used here that I haven't encountered before.  I hope
you'll excuse me asking instead of Googling them, but words like
list and value are tough to get the relevant sense of them for
these cases...
> class Payoffs(list):
>      def __init__(self, value=None):
>          list.__init__(self)

This class is a list that has methods?  That seems sort
of unusual to me.  Am I interpreting that right?

How is this class called?  With a list AND a value?  What
does it mean to initialize a list (the third line?).  

>          if value==None: # use a default prisoner's dilemma
>              value=[[(3,3),(0,5)],
>                     [(5,0),(1,1)]]
>          self.extend(value)

This means that the list that is the instantiation of this
class is being extended by the hardcoded values given
here.  But if value == None, was there any list to extend,
or was it an empty list, []?  Why not just pass a list and
to a class directly, and if not use a default list without
having to use .extend()?  There is no case here in which
a passed-in list would be extended with a hardcoded list,
>      def __repr__(self):

>          l1="Your Choice:   Cooperate    Defect\n"
>          l2="My choice:   -------------------------\n"
>          l3="Cooperate    | (% 3d,% 3d) | (% 3d,% 3d) |\n" % (self[0] 
> [0][0], self[0][0][1], self[0][1][0], self[0][1][1])
>          l4="              ----------------------- \n"
>          l5="Defect       | (% 3d,% 3d) | (% 3d,% 3d) |\n" % (self[1] 
> [0][0], self[1][0][1], self[1][1][0], self[1][1][1])
>          l6="             -------------------------\n"
>          return l1+l2+l3+l4+l5+l6

What is the reason for using __repr__() here and also
this |1 style?  I have not seen this before. 

>      def transpose(self):
> And that's where I'm at.  How can I have the transpose method return  
> another Payoffs object?  Here's the beginning of it:
>      def transpose(self):
>          trans=[[(self[0][0][1],self[0][0][0]), (self[1][0][1],self[1] 
> [0][0])],
>                 [(self[0][1][1],self[0][1][0]), (self[1][1][1],self[1] 
> [1][0])]]

Did this have to be hardcoded like this, or is there a more
Pythonic way to transpose the payoff list?  Maybe it needs
to look like this, but this does not strike me as very readable.

Thanks for any future insight,

Your E-mail and More On-the-Go. Get Windows Live Hotmail Free.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20091015/41dd22af/attachment.htm>

More information about the Tutor mailing list