Steve Howell wrote:
There are some object-oriented variants of Karel the Robot:
http://pclc.pace.edu/~bergin/karel.html
Guido van Robot and Webster van Robot are purely procedural, which
has the following benefits:
1) There's a lot less typing, which means more time for thinking,
exploring, etc. 2) You can master simple, but fundamental, concepts
like creating methods and nesting loops without the conceptual
overhead of object-oriented programming.
I would love to brainstorm ideas on creating a path from GvR and WvR
to more object-oriented programming. IMHO you need to make sure you
do it in a non-superficial way. For example, I would want your
object space to be rich enough that you actually have objects that
interface with each other.
The way it usually is presented in Logo is like:
TELL :robot [FD 10]
That is, you have some actors (activated with TELL), and you have
commands, and the commands are passed to the actors. There's a default
actor. Like everything else in logo, it is dynamically scoped, not
lexically, which makes methods and functions less distinct. That is, if
you do:
TO SQUARE :size
REPEAT 4 [FD :size RT 90]
END
TELL :robot [SQUARE 100]
then even though SQUARE is a procedure, it uses FD, and FD is a method.
Or, something like a method.
Anyway, it's not exactly applicable to Python, though it could be.
Here's a factoring of turtles:
class active_method(object):
def __init__(self, func):
self.func = func
def __get__(self, obj, type=None):
if obj is None:
# Maybe with some default too, like the turtle module lazily
# creates an instance
obj = type.active_turtles[-1]
return new.instancemethod(self.func, obj, type)
class Pen(turtle.Pen):
active_objects = []
def __enter__(self):
self.__class__.active_objects.append(self)
return self
def __exit__(self):
active = self.__class__.active_objects
assert active[-1] is self
active.pop()
@active_method
def fd(self):
whatever fd does...
... and so one, with more @active_method...
fd = Pen.fd
... and again, for every method ...
def square(size):
for i in range(4):
fd(size)
rt(90)
with Pen():
setxy(100, 10)
square()
--
Ian Bicking : [EMAIL PROTECTED] : http://blog.ianbicking.org
_______________________________________________
Edu-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/edu-sig