[Tutor] basic universe simulating for dummies

Daniel Yoo dyoo@hkn.eecs.berkeley.edu
Sun, 19 Nov 2000 11:12:09 -0800 (PST)


On Sun, 19 Nov 2000, sheri wrote:

> hello everybody! i am a newbie programmer wannabe and i am trying to
> learn to program in python by studying joe strout's basic universe
> simulator. its the only code i've ever seen that does something
> non-trivial and interesting that i think i could actually understand
> how it works. i have been rewriting it and i'm at the point where i
> can type look and see whats in a room. next step is to move from one
> room to another. anyway i have been looking at this code for a while


Just to make sure: the code that you're looking at appears to be here:

    http://www.strout.net/python/pub/pubcore.py


> # declare some constants
> the = 1
> a = 2
> The = 101
> A = 102

Here, the code appears to be assigning the important articles to numbers.  
From what I can tell, these numbers are completely arbitrary --- they just
have to be different from each other.


> # get name
>  def GetName(self, article=0):
>   if not article: return self.name
>   if article==the:
>    if hasattr(self,'the'): return self.the + ' ' + self.name
>    return 'the ' + self.name
>   if article==a:
>    if hasattr(self,'a'): return self.a + ' ' + self.name
>    return 'a ' + self.name 
>   if article==The:
>    if hasattr(self,'the'): return cap(self.the) + ' ' + self.name
>    return 'The ' + self.name
>   if article==A:
>    if hasattr(self,'A'): return cap(self.a) + ' ' + self.name
>    return 'A ' + self.name 
>    return self.name
> 
> 
> why assign different numbers? what does this code do?
> for me this is the most opaque part of the program except
> for the parser which i'm treating like a black box for now.


The numbers are different just so that we can say something like:

    myThing.getName(The)

instead of:

    myThing.getName("The")

It's easier to type.  That's pretty much what I can tell.  *grin* At the
same time, this stuff is tied slightly to the parser.  The parser appears
to call getName() with the proper article, depending on the kind of
message it'll needs to send.  We need to look at something like

    http://www.strout.net/python/pub/pubverbs.py

to find a reason why they're doing this sort of stuff.  Let's take a look:

    self.atsucc = 'You ' + x + ' at <the dirobj>.'
    self.atosucc = '<The actor> ' + x + 's at <the dirobj>.'

We can guess that "<the dirobj>" will be replaced with a call to
getName(the).  Likewise, "<The actor>" should be replaced with the results
of actor(The).  I'm not quite sure how or when the program does this
stuff, but that's probably the most likely reason why getName takes an
article.

Hope this helps!