[Tutor] Re: Help with classes

Kevin python.programming at gmail.com
Fri Apr 8 10:08:18 CEST 2005

Well this OOP stuff is realy hard for me as I have never even
programmed it took me a while just to understand defs. However I am
determined to learn how to do it. My biggest problem is with __init__
I still don't understand how to use it. Though I did try somthing
different with my code rather then having the dict of commands in the
main part of the code, I put it in th __init__ of class Comands like

class Command:
    def __init__(self):
        self.command_list = {}
        self.command_list['get'] = 'get'
        self.command_list['look'] = 'look'
        self.command_list['kill'] = 'kill'
    def UserCMD_get(self):
        print "Not in yet"

    def UserCMD_look(self):
        print "Not in yet"

    def UserCMD_kill(self):
        print "Not in yet"

test = Command()
while 1:
    prompt = raw_input(">>>: ")
    if prompt not in test.command_list:
        print "That is not a command"
    if prompt in test.command_list:
        exec 'test.UserCMD_' + prompt + '()'
though doing it like this to me the only real difference is the dict
of commands is in the __init__ of class. But why do I feel that this
is still the wrong way to do this. I was trying to look at other
examples but they are hard to fallow with no comments of any kind.
what my ultimate goal for right now is make it so that at the prompt
wich is >>>:
When a player type is "get sword" it will add a sword to there
inventory. Wich I'm not sure if I am going about this the right way.



On Apr 8, 2005 3:01 AM, Andrei <project5 at redrival.net> wrote:
> 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
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor

More information about the Tutor mailing list