[Tutor] Starting classes

Eric Brunson brunson at brunson.com
Fri Aug 31 21:48:37 CEST 2007


Ara Kooser wrote:
> 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):
>   

Not enough underscores, you need two before and after the word "init".

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

Why not:

outside1 = Area( "Outside", "You are standing outside..." )

and store self.description in the constructor?



> self.contents.append("dirt")
>   

What is self?  You've only defined self in the class methods and you're 
outside the class definition.  Was that just a cut and paste error? 

>
> look(bedroom)
>   

You'll get another error here, I  think you want:  outside1.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?
>   

No, that's correct, because you are instantiating the class and naming 
that instance "outside1".

> Thank you.
>
> Ara
>
>
>
>   

Hope that all helps,
e.



More information about the Tutor mailing list