[Tutor] Further into classes

Ara Kooser ghashsnaga at gmail.com
Sun Sep 2 00:23:56 CEST 2007


Thank you for the help on getting started on classes. I have a basic
understanding of how they are used and I reread the tutorials again.
So I have a class set up and running. My question is how you append a
list (such as self.contents = [ ]) using a method like def AddObject?
The code I am trying is below but it is not working. I am really sure
I am not going about this the right way. Thank you.

Ara

#####################################################################
#Text Advenuture
#By Ara Kooser
#Thanks to Chris, e., and Steven at python tutor
####################################################################

class Area:

    #What makes it an area?
    def __init__(self, name, description):
        #Number of arguements in the _init_ must be defined
        self.name = name
        self.description = description
        self.contents = []

    #Methods. What you can do.
    def AddObject(self,thing):
        #pass
        self.contents.append()

    def look(self):
        print "Look around the place you are in"
        print "You are in the",self.name
        print self.description

    def search(self):
        print "You search the area and find..."
        print self.contents

first_instance = Area("Outside", "You are standing outside")
first_instance.AddObject("Stick")
first_instance.look()
first_instance.search()



second_instance = Area("Inside", "You are standing inside")
second_instance.AddObject("Iron pot")
second_instance.look()
second_instance.search()





-- 
Quis hic locus, quae regio, quae mundi plaga. Ubi sum. Sub ortu solis
an sub cardine glacialis ursae.


More information about the Tutor mailing list