[Tutor] Re: Help with classes

Andrei project5 at redrival.net
Fri Apr 8 09:01:39 CEST 2005


Bob Gailer <bgailer <at> alum.rpi.edu> writes:

> At 12:22 PM 4/7/2005, Andrei wrote:
> >Kevin <python.programming <at> gmail.com> writes:
> >
> > > I am fooling around with classes and I was trying to create a very
> > > small one player text adventure. I made a class called commands here
<snip>
> >I don't think you're making proper use of classes.
>
> IMHO there is no "proper use of classes".

Perhaps I didn't phrase that quite the way I intended. What I meant is that
there are places where classes are obviously beneficial and there are places
where a different solution is the easier one. Python is fortunately flexible
enough to allow all kinds of approaches.

> In Python a class is whatever one creates using the class statement. In 

As it is in all languages. Which is not to say that any class the language
allows you to make is actually useful or the best way to do the job. Python has
with its tuples, lists, dicts and sets a whole bunch of collection types readily
available, which look like collections and are instantly recognized by any
programmer as collections (unlike a class, which I for one would expect to be
used as more than a collection when reading code).

<snip>
> Kent's proposal makes IMHO excellent use of the class mechanism. One may 
> also access the class __dict__ directly in lieu of using getattr.

Yep, hence my statement that a class in this use case is just a confusing way of
using a dict :). I read (perhaps misread) Kevin's post as a question from
someone who is new to OOP and wants to learn about it, rather than a request on
info regarding the manipulation of class internals. 

> >The Commands class is merely a collection of unrelated methods.
> 
> But they are related. Each represents a game command; the class is the 
> container for the commands. And it might be that as the program expands 
> that there would be multiple instances representing players or saved games 
> or ??? And class properties would keep track of the player's status.

Guessing at Kevin's intentions is quite difficult by other means than reading
the posted code. A class called "Commands" that only contains methods
implementing actions as results to commands given by the human player, can't
reasonably be interpreted as something that will at some point hold a
character's status.

Yours,

Andrei



More information about the Tutor mailing list