Why use Slot? from Peter Norvig's AI code

Discussion in 'Python' started by Davy, Dec 4, 2007.

  1. Davy

    Davy Guest

    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
     
    Davy, Dec 4, 2007
    #1
    1. Advertising

  2. 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
     
    Bruno Desthuilliers, Dec 4, 2007
    #2
    1. Advertising

  3. Davy

    Terry Reedy Guest

    "Davy" <> wrote in message
    news:...
    | 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
     
    Terry Reedy, Dec 4, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Davisro
    Replies:
    3
    Views:
    405
    bruce barker
    Apr 29, 2004
  2. Davisro
    Replies:
    1
    Views:
    389
    George Ter-Saakov
    May 3, 2004
  3. Roedy Green

    Peter van der Linden

    Roedy Green, May 7, 2004, in forum: Java
    Replies:
    7
    Views:
    537
    marcus
    May 10, 2004
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,970
    Smokey Grindel
    Dec 2, 2006
  5. Carnell, James E
    Replies:
    2
    Views:
    237
    Marc 'BlackJack' Rintsch
    Sep 5, 2007
Loading...

Share This Page