[Tutor] Two More Questions

Daniel Yoo dyoo@hkn.eecs.berkeley.edu
Tue, 27 Feb 2001 23:38:36 -0800 (PST)

On Tue, 27 Feb 2001, Britt Green wrote:

> cupboard = Classes.Containers("cupboard", 0, "closed")
> key = Classes.Items ("worn key", 0)
> kitchen = Classes.Rooms("Kitchen")
> However, when I run the above code, Python replies with this:
> >>>
> Traceback (innermost last):
>   File "C:/Program Files/Python20/game.py", line 7, in ?
>     kitchen = Classes.Rooms("Kitchen")
> AttributeError: Rooms

Strange!  I tried out your code, and didn't run into any problems.  Can
you recheck your work?

While you're in the interpreter, if you start making changes to
Classes.py, you need to tell the interpreter that your module has
undergone changes.

Doing the obvious:

>>> import Classes

doesn't work; as an optimization step, if Python sees that a module's
already loaded, it'll ignore any more 'import Blah' calls.  So we really
do need to force the issue, by using reload():

>>> reload(Classes)
<module 'Classes' from 'Classes.pyc'>

That's a wild shot in the dark, but perhaps this is what's happening for

> Also, I was wondering how I could put a fairly large string into the
> constructor. If I had a couple of paragraphs of a room description,
> what would it look like when I called the constructor? I haven't been
> able to figure out how to get Python to accept multi-line strings this
> way.

Python supports multi-line strings, which are very nice.  For example, we
could do this:

roomdesc = """It is pitch dark.

You are likely to get eaten by a grue.

Triple quotes begin and end a multi-line string; otherwise, they're not
different from regular strings.  They can be passed around as parameters:

>>> string.center('''hello world.
... this is a test.''', 45)
'         hello world.\012this is a test.        '

Hmmm... but it depends on the function if it handles a multi-line string
properly or not.  string.center() certainly didn't handle that nicely.  

If you want to take a look at the gory details, here's the link to the
reference manual:


Hope this helps!