Why use Slot? from Peter Norvig's AI code

Terry Reedy tjreedy at udel.edu
Tue Dec 4 12:24:20 EST 2007


"Davy" <zhushenli at gmail.com> wrote in message 
news:70161450-5ea7-4527-9228-70b397dce0af at d27g2000prf.googlegroups.com...
| 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






More information about the Python-list mailing list