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

Steve Willoughby steve at alchemy.com
Thu Oct 15 20:14:29 CEST 2009

On Thu, Oct 15, 2009 at 01:33:51PM -0400, Che M wrote:
> > 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?

Lists have methods, like any other class of objects.
For example, list.append().  What you have here is a new
type of data collection called Payoffs, which is a sub-class
of list.  IOW, it's a specialized kind of list which has
all the normal behavior of lists, plus some special stuff

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

Payoffs, unlike regular lists, interpret the values in them
so an initialization makes some sense.  And regular lists
are (or can be) initialized with a value parameter too:

a = list([1,2,3,4])

Not that you'd normally write that exactly like that,
of course, but you might do that to create, say, a list
from a tuple of values:

a = list(some_tuple)

So really this is just an extension of that idea.

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

So just saying
	a = Payoffs()
gets you a default arrangement, but you could
specify something different if you wanted.

> 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,

This starts out as a regular list object (note the call
to list.__init__() at the start of Payoff's __init__),
so it'll be [] by default.

> 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,
> correct?

This could have been a stand-alone class which contained
a list attribute, yes.  When designing a class you have to
consider whether you get a better design by extending a
class (particularly like here when it only really has
a single value which is already very similar to another
type), or by starting a new class which contains other
objects as attributes.

> >      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. 

It's usual for a class to define __str__ and/or __repr__ so that you get
a useful string representation of an instance of that class.  Here the
object explains what it represents more clearly than just saying "it's
a bunch of numbers in a list."

Steve Willoughby    |  Using billion-dollar satellites
steve at alchemy.com   |  to hunt for Tupperware.

More information about the Tutor mailing list