[Tutor] Fw: Starting classes

christopher.henk at allisontransmission.com christopher.henk at allisontransmission.com
Fri Aug 31 22:58:13 CEST 2007


Oops, forgot to reply all.

Chris Henk
----- Forwarded by Christopher Henk/US/ATD/GMC on 08/31/2007 04:55 PM 
-----

Christopher Henk/US/ATD/GMC
08/31/2007 04:13 PM

To
"Ara Kooser" <ghashsnaga at gmail.com>
cc

Subject
Re: [Tutor] Starting classes





The class definition will only have the methods and/or the variables in 
its definition.
So these two functions make up your class
class Area:
    def _init_(self, name, description):
                self.name = name

    def look(here):
        "Look around the place you are in"
        print here.description

looking at the class code:
your init function only has one underscore before and after init, you want 
two, that is why you are getting the error.
def __init__(self, name, description):

Also you are requiring two arguments to make an Area class, the name and 
the description.
If you try to create an instance using only the name (as you do below) the 
interpreter will again raise an error. 

 def look(here):
        "Look around the place you are in"
        print here.description

Not sure if this works as is, I believe it depends on the interpreter, but 
it is customary to use the word self as the first parameter, and then also 
change here in the function body 
 def look(self):
        "Look around the place you are in"
        print self.description

Getting into the code below the class definition:
>outside1 = Area("Outside")

This will raise an error since you required two parameters to create an 
Area class and you only provide one.
You can either add the description here or leave it off in the __init__ 
function. 
Since most likely every area will have a description , I would add it 
here.
outside1 = Area("Outside","You are standing outside with the town gate to 
your back")

outside1.description = "You are standing outside with the town gate to
your back"

(see above, and below)

self.contents.append("dirt")

Here we have two problems.  The first is the class doesn't have the 
"contents" attribute yet  That's only a problem since you are trying to 
call a function with it. 
When you assign to an attribute (like with "description" in the line 
above), you are adding the attribute to you class instance ("outside1"), 
however you need to add it before you can use it.
Second, you only use self from within the function definition.  In this 
case you would want to use outside1.contents.

look(bedroom)

Since look was defined in the class definition, you would want to call it 
like this.
bedroom.look()

But as of right now there is no bedroom created, so this will give an 
error.
however you can instead look outside.
outside1.look()


Looking at your code I would have a class something like this:
class Area:
        #what does an area have that makes it an area?
        contents=None 
        name=None 
        description=None 
        paths=None      #where you can go

        def __init__(self, name, description):
                #define what everything will start as
                self.name = name
                self.discription=None 
                self.contents=[]
                self.paths={}
 
        #Methods:
        #what can you do to an Area?
 
        def look(self):
        "Look around the place you are in"
        print self.description
 
        def addSomething(self,thing):
        "adds something to the contents"

        def removeSomething(self,thing)
        "remove from contents"
 
        def addPath(self,direction,area)
        "Adds a connection to area"
 
        #...
        #more as needed
 
 

Chris Henk



"Ara Kooser" <ghashsnaga at gmail.com> 
Sent by: tutor-bounces at python.org
08/31/2007 03:06 PM

To
tutor at python.org
cc

Subject
[Tutor] Starting classes






Hello,
  I read Alan Gauld's and How to Think Like a Computer Scientist
section on classes. So I tried to write a simple room class. My goal
is to write a short text adventure using classes. Here is the code:

class Area:
    def _init_(self, name, description):
        self.name = name


    def look(here):
        "Look around the place you are in"
        print here.description


outside1 = Area("Outside")
outside1.description = "You are standing outside with the town gate to
your back"
self.contents.append("dirt")


look(bedroom)

I get the following error.
Traceback (most recent call last):
  File "/Users/ara/Documents/text_advent.py", line 11, in <module>
    outside1 = Area("Outside")
TypeError: this constructor takes no arguments

Do the outside1 = Area("Outside) need to be nested in the class or can
they be outside of it?

Thank you.

Ara



-- 
Quis hic locus, quae regio, quae mundi plaga. Ubi sum. Sub ortu solis
an sub cardine glacialis ursae.
_______________________________________________
Tutor maillist  -  Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070831/9352fc12/attachment-0001.htm 


More information about the Tutor mailing list