[Tutor] Function assignment (was: Re: Function type?)

Zak Arntson zak@harlekin-maus.com
Wed Jun 11 12:03:59 2003


> Yes, very possible.  The makeMsg() function above is an example of a
> function that itself generates function objects.  You might be able to
> employ the same function-constructing technique when attaching new
> description functions to your objects.

I still run into the problem of defining a function first, then assigning
it to a property/method afterwards. I don't want to force developers to
create a uniquely-named "dummy" function every time.

For example, currently (as far as I know), I have to implement adding a
function like so:

###

# Function Description

pipe = Item('pipe')
def pipe_desc():
    if self.parent = player:
         return "In your hand."
    return "On the floor."
pipe.desc = pipe_desc

# String Description (for comparison)

pipe2 = Item('pipe')
pipe2.desc = "A boring pipe, with no changing description."

###

Which means there's this pipe_desc object floating around, and could
accidentally be overwritten. So I have to force a naming convention, such
as itemname_function. Which would work, but feels kludgy and isn't as
elegant.

I'm jealous of Inform (Graham Nelson's interactive fiction language,
http://www.inform-fiction.org), where you can create multi-line dynamic
functions or strings, without much concern. Though I'm pretty sure a
"blah" string given to a description actually evaluates to a function
equivalent
to
   print "blah^";  ! '^' is a equivalent to Python's '\n'
   rtrue;   ! return true (or false, I can't remember)
Here's the code:

### (Inform, not Python :)

! Function Description

Object pipe
  has   name 'pipe',
        description [;
            if (self in player)
                print_ret "In your hand.";
            else
                print_ret "On the floor.";
        ],
;

! String Description (for comparison)

Object pipe2
  has   name 'pipe',
        description "A boring pipe, with no changing description.",
;
###

> Your project sounds extraordinarily dynamic!  *grin* Please show us more
> of it when you have the chance; it looks like a lot of fun.

I will. I plan on releasing the thing (if it ever gets done) completely
open. I've got the parsing state machine mostly done, it's got something
like 15+ states and parses things out into subjects, verb, direct objects
(and any accompanying prepositions and adjectives), indirect objects
(again, preps and adj's). Pretty neat stuff, and easy with Python.

By the way, I'm naming my module PyFict ... am I stepping on any toes with
that?

> Good luck!

Thanks!

-- 
Zak Arntson
www.harlekin-maus.com - Games - Lots of 'em