Why use Slot? from Peter Norvig's AI code

D

Davy

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
 
B

Bruno Desthuilliers

Davy a écrit :
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).

First point : this code seems to be based on an older (way older) Python
version, so some things would not be done that way no more. Also, in
newer (well... not that new, but...) Python versions, the term 'slot'
has a quite different meaning (for short: a memory optimization for
attributes...). FWIW, what the author names 'slots' here are really
instance attributes - not the use of inner functions (please refer to
the class's docstring and __init__ method code).
Please see "program()" nested in "make_agent_program()",
why not use program() directly?

It's a local variable of make_agent_program, so unless you bind it to
another name (which is done in the __init__), you can't access it from
outside make_agent_program.

The intent - which is explained in the docstring - is to make sure the
'program' function won't access the Agent instance - so it was obviously
written for a Python version that predates lexical closures support (you
can do some archeological research to find out when this support was
added if you want !-).

Now this implementation, whatever it was worth by the time this code was
written (some 5 or more years ago AFAICT) would be considered a WTF with
newer Python versions - where the obvious solution would be to define
"program" as a staticmethod.



HTH
 
T

Terry Reedy

| 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?

To expand on BD's comment: make_agent_program() returns a new function
object for each instance. If each is a copy of the same func, that makes
little sense in itself. PN could just as well have put defined programs()
outside of the class and passed it to the constructor or grabbed it as a
global. But perhaps PN intends that in a real program, the .program
attribute would not always be the same.

The more subtle point is that a function that is an attribute of a class
normally get an instance as the first parameter while a function that is an
attribute of a instance does not. As BD said, use of @staticmethod() would
have the same effect, today, if indeed .program was really meant to be the
same function for all instances.

Terry Jan Reedy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top