#!/usr/bin/python
#
# Author: Shannon -jj Behrens
# Date: Fri Mar 3 16:43:43 PST 2006
#
# In response to: http://secretartofscience.com/blog/?p=8
#
# Although Python isn't a prototype-based language, it's possible to do a lot
# of these same wacky ideas in Python.
#
# By the way, Io is a cool, new prototype based language. I've blogged about
# it before. There was also a knockoff of Python that was prototype based, but
# I don't remember the name--email me if you must know.
#
# Ok, let's start by changing our class on the fly.
class Draws(object):
def draw(self):
print "draw"
class DrawsSmall(Draws):
def draw(self):
print "small:",
Draws.draw(self)
obj = Draws()
obj.draw()
obj.__class__ = DrawsSmall
obj.draw()
# This time, let's mixin something on the fly.
class DrawsBig(Draws):
def draw(self):
print "big:",
Draws.draw(self)
obj = Draws()
obj.draw()
class MixedIn(DrawsBig, Draws): pass
obj.__class__ = MixedIn
obj.draw()
# This time, do it dynamically. I'll leave it as an exercise to the reader to
# implement "replace_parent_class".
import random
def inject_parent_class(obj, Class):
NewClass = type("DynamicGeneratedClass",
(Class,) + obj.__class__.__bases__, {})
obj.__class__ = NewClass
obj = Draws()
obj.draw()
Class = random.choice([DrawsBig, DrawsSmall])
inject_parent_class(obj, Class)
obj.draw()
In his keynote at PyCon, Eben Upton, the Executive Director of the Rasberry Pi Foundation, mentioned that not only has Minecraft been ported to the Rasberry Pi, but you can even control it with Python . Since four of my kids are avid Minecraft fans, I figured this might be a good time to teach them to program using Python. So I started yesterday with the goal of programming something cool for Minecraft and then showing it off at the San Francisco Python Meetup in the evening. The first problem that I faced was that I didn't have a Rasberry Pi. You can't hack Minecraft by just installing the Minecraft client. Speaking of which, I didn't have the Minecraft client installed either ;) My kids always play it on their Nexus 7s. I found an open source Minecraft server called Bukkit that "provides the means to extend the popular Minecraft multiplayer server." Then I found a plugin called RaspberryJuice that implements a subset of the Minecraft Pi modding API for B
Comments