Noob: What is a slot? Me trying to understand another's code

Carnell, James E jecarnell at saintfrancis.com
Tue Sep 4 14:03:16 EDT 2007



I am thinking about purchasing a book, but wanted to make sure I could
get through the code that implements what the book is about (Artificial
Intelligence a Modern Approach). Anyway, I'm not a very good programmer
and OOP is still sinking in, so please don't answer my questions like I
really know anything.

MY QUESTION:
What is a slot? In class Object below the __init__ has a slot.  Note:
The slot makes use of a data object called 'percept' that is used in the
TableDrivenAgent(Agent) at the bottom of this post. I am guessing this
is a type of Finite State Machine (I haven't bought the book yet so I am
guessing).

MY GUESS AT THE ANSWER:
I've played around with it, and my guess is that is just some data
container local to the instance of Object (after it has been extended or
inherited or whatever). In other words, if I would have made some global
dictionary that each instance of Object and or Agent(Object) could
access it would be possible to allow instances of other classes to
access the global dictionary.

Why not make a class instance have something like a dictionary for
keeping it's own records like self.myListOfActions then you could append
to it etc. I guess it wouldn't be private, but I don't think anything
really is private in python.

Anyway, why a slot (whatever that is)?


class Object:
    """This represents any physical object that can appear in an
Environment.
    You subclass Object to get the objects you want.  Each object can
have a
    .__name__  slot (used for output only)."""
    def __repr__(self):
        return '<%s>' % getattr(self, '__name__',
self.__class__.__name__)

    def is_alive(self):
        """Objects that are 'alive' should return true."""
        return hasattr(self, 'alive') and self.alive

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


##################### HERE IS THE SLOT #######################

    def __init__(self):
        def program(percept):
            return raw_input('Percept=%s; action? ' % percept)
        self.program = program
        self.alive = True

################THIS APPEARS LATER IN THE PROGRAM##############
< so you can see where the 'percept' is coming from and how it is being
used.

class TableDrivenAgent(Agent):
    """This agent selects an action based on the percept sequence.
    It is practical only for tiny domains.
    To customize it you provide a table to the constructor. [Fig.
2.7]"""
    
    def __init__(self, table):
        "Supply as table a dictionary of all {percept_sequence:action}
pairs."
        ## The agent program could in principle be a function, but
because
        ## it needs to store state, we make it a callable instance of a
class.
        Agent.__init__(self)
        ################################### percept ##########
        percepts = []
        def program(percept):
            percepts.append(percept)
            action = table.get(tuple(percepts))
            return action
        self.program = program


Thanks for your time (and patience),

James Carnell





More information about the Python-list mailing list