Skip to content

Instantly share code, notes, and snippets.

@wakhub
Created September 16, 2012 13:11
Show Gist options
  • Save wakhub/3732400 to your computer and use it in GitHub Desktop.
Save wakhub/3732400 to your computer and use it in GitHub Desktop.
Prototype based OOP in Python
import new
class ProtoObj(object):
def __init__(self, prototype={}):
self.prototype = prototype
self.__super_prototype = {}
def clone(self):
Obj = new.classobj('ProtoObjClone', (ProtoObj, ), {})
instance = Obj()
instance.__super_prototype = self.prototype
instance.prototype = {}
return instance
def __getattr__(self, name):
if name in self.__dict__:
self.__dict__[name]
# prototype chain
prototype = None
if name in self.prototype:
prototype = self.prototype
elif name in self.__super_prototype:
prototype = self.__super_prototype
if prototype:
value = prototype[name]
if type(value).__name__ == 'function':
return lambda *args: apply(value, [self] + list(args))
return prototype[name]
def __setattr__(self, name, value):
if type(value).__name__ == 'function':
self.__dict__[name] = lambda *args: apply(value, [self] + list(args))
return
self.__dict__[name] = value
return
MyObj = ProtoObj({
'prop1': "ABC",
'prop2': 1,
'method1': lambda self: self.prop1 * 2,
'method2': lambda self: self.prop2 * 3,
})
MyObj2 = ProtoObj({
'method1': lambda self: "FOOOOO",
'method2': lambda self: "BAAAAR",
})
obj1a = MyObj.clone()
obj1a.prop1 = "AAA"
obj1b = MyObj.clone()
obj1b.prop1 = "BBB"
obj2 = MyObj2.clone()
print(obj1a.method1()) # AAA
print(obj1a.method2()) # 3
print(obj1b.method1()) # BBB
print(obj1b.method2()) # 3
MyObj.prototype['method1'] = lambda self: "HOGEHOGE"
obj1b.prototype['method1'] = lambda self: "FUGAFUGA"
obj1c = MyObj.clone()
print(obj1a.method1()) # HOGEHOGE
print(obj1b.method1()) # FUGAFUGA
print(obj1c.method1()) # HOGEHOGE
obj1a.method1 = lambda self: self.prop1 + "FUGAFUGA"
print(obj1a.method1()) # AAAFUGAFUGA
print(obj2.method1()) # FOOOOO
print(obj2.method2()) # BAAAAR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment