By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,246 Members | 2,126 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,246 IT Pros & Developers. It's quick & easy.

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

P: n/a


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
Sep 4 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Carnell, James E wrote:
>
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).
I really have a hard time grasping what it is you don't understand (the
annoying thing about not understanding stuff is that you usually lack
the proper terms to explain what you don't understand, precisely
_because_ you don't understand it ;)).

At first I thought you were talking about __slots__, as explained in the
docs <URL:http://docs.python.org/ref/slots.html>.
[but some snipped code later you say:]

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

def __init__(self):
def program(percept):
return raw_input('Percept=%s; action? ' % percept)
self.program = program
self.alive = True
That is "simply" a special method (namely the one that is called after
instance creation so you can set up the instance variables).
<nagging>You should really know that.<\nagging>
Lookie here: <URL:http://docs.python.org/ref/customization.html>

Did that do it? Sorry, I have big trouble looking at long listings and
figuring out their meaning. If that didn't help, try reformulating your
question (please).

/W
Sep 4 '07 #2

P: n/a
On Tue, 04 Sep 2007 13:03:16 -0500, Carnell, James E wrote:
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).

[…]

Anyway, why a slot (whatever that is)?

[Example code snipped.]
If you want to learn Python don't do it with that book because the authors
seem to write another programming language using Python syntax. I guess
there's an older issue of that book that used the SmallTalk programming
language and they switched to Python syntactically but not mentally and not
the vocabulary. A "slot" in SmallTalk is called "attribute" in Python.

Ciao,
Marc 'BlackJack' Rintsch
Sep 5 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.