[Tutor] Re: Help with classes

Alan Gauld alan.gauld at freenet.co.uk
Fri Apr 8 11:33:08 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.

That's OK, OOP is quite a strange concept for many folks. Its
actually easier to learn as a beginner than for folks who
have been programming without OOP for a long time!

> determined to learn how to do it. My biggest problem is with
__init__
> I still don't understand how to use it.

init is simply where you initialise the data attributes of your class.

It could be defined as a normal method and you call it explicitly:

class Message:
  def init(self, txt = 'Hello world'):
     self.text = txt
  def sayIt(self):
     print self.text

And every time you create a message object you explicitly call it:

m = Message()
m.init('Hiya fred!')
m.sayIt()


The __init__ style is simply a way for Python to do some of the work
by calling the init method immediately after it creates the new,
blank object. So

class Message:
  def __init__(self, txt = 'Hello world'):
     self.text = txt
  def sayIt(self):
     print self.text

Does exactly the same as the first version except that we no longer
call init explicitly abnd instead pass the message text into the
instantiation call:

m = Message('hiya fred!')
m.sayIt()

But there is nothing mysterious, its just a way to initialise the
internal data at the time you create an instance of the class.

> 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
> so:
>
> class Command:
>     def __init__(self):
>         self.command_list = {}
>         self.command_list['get'] = 'get'

You are not actually storing your methods(function objects) here, you
are simply storing the name(a string) which is the same as the key.

>     def UserCMD_get(self):
>         print "Not in yet"

So you need to do:

       self.command_list['get'] = self.UserCMD_get   # no parens!

To store a reference to the actual function UserCMD_get in your
dictionary.
However, as Kents mail pointed out this isn't necessary since Python
already uses a dictionary of its own to store these things so you can
use get_attr() etc to do the same job. And as Andrei said you can do
it
even more simply without a class at all, just using functions. If you
really want to use classes then having one per command is a better
approach. The commands are all individual objects that you can use.

> 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 + '()'

And you don't need to use exec, all you need to do is call
the function object in the dictionary directly:

      test.command_list[prompt]()

test.command_list['prompt'] will fetch the function object from
the dictionary and the parens () at the end calls the function.

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

Its probably easier to use a non OOP style for the menu and commands
and use the OOP stuff for the objects within the game - like Player,
Character, Sword etc. But you have to start thinking about the objects
not the functions. The objects are "alive" and communicating with each
other via messages. Each message is a call to a method of a class.

Its worth trying to sketch out how these objects would be related to
one another in a diagram, nothing fancy just some boxes and lines.
Maybe use arrows to show what direction messages flow in - ie which
object sends which message to which other object. Also an adventure
game is quite a duifficult starting point for OOP.

Maybe trying something simpler first like a shopping list creator
say. There each item you might want to buy can be an object that
knows how to print itself and its price. The ShoppingList itself
is just a collection of such objects and when you print it it asks
each object to print itself and return the price...
The ShoppingList completes the list by printing the total price.

Many of the concepts are similar to what you are doing here but
the overall program is much simpler in concept.

Good luck!

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld



More information about the Tutor mailing list