Re: finding name of instances created

2005-01-21 Thread Andr Roberge
Craig Ringer wrote:
On Fri, 2005-01-21 at 16:13 -0800, Andr wrote:
Short version of what I am looking for:
Given a class public_class which is instantiated a few times e.g.
a = public_class()
b = public_class()
c = public_class()
I would like to find out the name of the instances so that I could
create a list of them e.g.
['a', 'b', 'c']
[snip]

I'm not really able to grasp what you're trying to do (but others
might). It wouldn't hurt if you could post a description of what you're
actually trying to achieve - /why/ you want this - as that can often be
very helpful both in understanding what you're thinking and in
suggesting a suitable approach or alternative.
Ok, here it goes... I am designing a learning environment for Python.
(See rur-ple.sourceforge.org for details of a *very early, still buggy* 
relase).  I have a world in which a
robot can accomplish four built-in instructions: move(), turn_left(), 
pick_beeper(), put_beeper().
turn_left() corresponds to a 90 degree left turn.  One can define a 
function to simulate a 90 degree right turn as follows:

def turn_right():
turn_left()
turn_left()
turn_left()
and call it as a built-in instruction thereafter.
By giving more and more complicated tasks for the robot to accomplish,
one can learn various programming concepts using python syntax:
def (as above), while, if, else, elif, ..
I have all of that working well so far (not on sourceforge yet).
Next, I want to introduce
the concept of classes and objects, again using python's syntax.
Behind the scene, I have something like:
robot_dict = { 'robot' = CreateRobot( ..., name = 'robot') }
and have mapped move() to correspond to
robot_dict['robot'].move()
(which does lots of stuff behind the scene.)
I have tested robot_dict[] with more than one robot (each with
its own unique name)  and am now at the point where I would like
to have the ability to interpret something like:
alex = CreateRobot()
anna = CreateRobot()
alex.move()
anna.move()
etc. Since I want the user to learn Python's syntax, I don't
want to require him/her to write
alex = CreateRobot(name = 'alex')
to then be able to do
alex.move()
I have tried various things at the interpreter, found that
to a class 'a', I could see the instance 'b' created in
locals():
'a': class '__main__.a', 'b': __main__.a object at 0x011515D0
which tells me that there must be a way to catch b's name as it is
created, and do what I want to do.
Does this clarify what I am trying to do and why?
Andr
--
http://mail.python.org/mailman/listinfo/python-list


Linguistic challenge: name this program

2004-12-05 Thread Andr? Roberge
In 1981, Richard Pattis wrote a delightful little book titled Karel
the Robot, a Gentle Introduction to the Art of Programming. Pattis's
Karel the Robot was named after the author Karel Capek, who
popularized the word robot in his play Rossum's Universal Robots.
Pattis's approach was to introduce a robot who could follow 5 basic
instructions and be taught to accomplish tasks of increasing
complexity.

A few years ago, a first implementation of Karel the Robot in Python
was created and called PyKarel. A second, newer implementation is
called Guido van Robot (GvR for short), and is available at
gvr.sourceforge.net. Work is currently underway by the developpers of
GvR to produce a new-and-improved version.

I have been working on my own (better ;-) version (sometimes
collaborating with the GvR folks) in order to learn Python. It is now
90% finished. It is meant to be a complete environment to learn about
programming concepts, from simple sequences of instruction to OOP.

Given the origin of Pattis's name (Rossum's Universal Robot) and the
name of Python's BDFL, I find it difficult to think of a better name
than Guido van Robot to name a programming environment in which one
uses Python to teach a robot new tricks! (Hat's off to Steve Howell
for this one). Yet, I want a clever name for my version.

Any suggestions?

Andre Roberge
-- 
http://mail.python.org/mailman/listinfo/python-list


exec and global puzzle

2004-12-05 Thread Andr? Roberge
I have the following two files:

#--testexec.py--
def exec_code(co):
try:
exec co
except:
print error

#-- test.py--
import thread
import testexec
import time

code = def a():\n print 'a'\n\n +\
   def c():\n a()\n\nc()

code2 = def a():\n print 'a'\n\n +\
   def c():\n global a\n a()\n\nc()

print  exec code - no global
exec code
print  exec from thread - no global
thread.start_new(testexec.exec_code, (code,))
time.sleep(1)
print \n exec code2 - with global
exec code2
print  exec from thread - with global
thread.start_new(testexec.exec_code, (code2,))
#---

Here's the output when I execute test.py:

 exec code - no global
a
 exec from thread - no global
error

 exec code2 - with global
a
 exec from thread - with global
a
#-
Without the global statement, I get an error when trying to execute
the code.
I don't understand why I need to use the global statement within the
definition of c() in order for it to know what a() is.  If I define
exec_code() within test.py and use it there, I do not get any error,
with or without the use of a global statement.

Andre
-- 
http://mail.python.org/mailman/listinfo/python-list