Why use Slot? from Peter Norvig's AI code

Davy zhushenli at gmail.com
Tue Dec 4 06:01:21 EST 2007


Hi all,

When reading Python source code of Peter Norvig's AI book, I found it
hard for me to understand the idea of slot (function nested in
function). Please see "program()" nested in "make_agent_program()",
why not use program() directly?

## http://aima-python.googlecode.com/svn/trunk/agents.py
class Agent (Object):
    """An Agent is a subclass of Object with one required slot,
    .program, which should hold a function that takes one argument,
the
    percept, and returns an action. (What counts as a percept or
action
    will depend on the specific environment in which the agent
exists.)
    Note that 'program' is a slot, not a method.  If it were a method,
    then the program could 'cheat' and look at aspects of the agent.
    It's not supposed to do that: the program can only look at the
    percepts.  An agent program that needs a model of the world (and
of
    the agent itself) will have to build and maintain its own model.
    There is an optional slots, .performance, which is a number giving
    the performance measure of the agent in its environment."""

    def __init__(self):
        self.program = self.make_agent_program()
        self.alive = True
        self.bump = False

    def make_agent_program (self):

        def program(percept):
            return raw_input('Percept=%s; action? ' % percept)
        return program

    def can_grab (self, obj):
        """Returns True if this agent can grab this object.
        Override for appropriate subclasses of Agent and Object."""
        return False


Best regards,
Davy



More information about the Python-list mailing list