[Tutor] designing POOP

Ricardo Aráoz ricaraoz at gmail.com
Tue Feb 12 13:19:50 CET 2008

Tiger12506 wrote:

> This is all fine and dandy, but the video game is pretty worthless unless it 
> can show us what the score is. There are two ways to go about this. A) Give 
> the video game a display which it updates, or B) Tear open the case of the 
> video game and look at the actual gears that increment the score to read it. 

Mmmm... that's not really so. If the object's creator meant the score to
be read that way then it would not be 'Tear open the case' but just
'read the score'. The language with which we describe an action does matter.

> vg = VideoGame()
> howmany = rand.randint(0,100)
> for i in range(howmany):
>   vg.buttonpush()
>   print vg.score        #Tear open the case (hope you have a screwdriver)
> OR
> class VideoGame():
>   def __init__(self):
>     self.score = 0
>   def updatedisp():
>     print self.score
>   def buttonpush():
>     self.score += 1
>     self.updatedisp()
> vg = VideoGame()
> howmany = rand.randint(0,100)
> for i in range(howmany):
>   vg.buttonpush()                #Let the videogame display your score 
> however it wishes
> The second way is preferable for many reasons...
> A) The game designer decides to change the display, you don't have to change 
> any code that uses the class
> B) Clearly, tearing open a videogame is pretty low-level from an object 
> perspective. This is what Alan is saying with OOP purism.

Did we think about REUSABILITY? What if in some other application I want
to USE the score, not just display it? What if I want to display it in a
different form (multiplying it by 100)? Then you are back to our
original options : either check the score directly, define a getter, or
a .... 'stater'(?) which returns the state of the object (in this case
it would be a tuple with only the score in it, a getter in disguise if
you ask me). AFAIK the python way is to just check the score, if later
the object's author changes something he has ways built in in the
language to keep the interface unchanged (yes, I think the score would
be part of the interface, otherwise it would be _score).

More information about the Tutor mailing list