My initial approach was a
__getattr__ method that consisted simply of
return getattr(self.prototype, name). When reattaching a new method to an instance, I needed an extra wrapper, done through a
wrap method which consisted of
return new.instancemethod(method, self, self.__class__). It would be used like this:
obj.method = obj.wrap(some_func).
this variable, which works whether the function is bound to some specific object or not. (Even global functions are bound to the global object.) But Python doesn’t have such a keyword. The language prefers the explicit, and uses a explicitly passed parameter, called by convention
self. The call to wrap a specific function also had the effect of binding the
self parameter to that particular object; even if it later became a prototype for some other object. This manifested itself as all kinds of broken behavior. For example, the original code has a global
scope object, and every time a new scope was entered, the global pointed to a newer object that kept a reference to the rest of the scope chain. But in the object’s methods,
self pointed to something different than the global. Messy.
Before I get into solutions, I’d like to see what readers say. How would you go about implementing prototypical inheritance in Python? And what is a more pythonic way to accomplish the same thing? Comment below. Thanks! -m