pythonでPrototype的な実装をしてみる

pythonprototype.js的な実装をしてみた


#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys

class Prototype: # Abstract Class
def __init__(self):
self.__dict__ = {}

def __setattr__(self, key, value):
self.__dict__[key] = value

def __getattr__(self, key):
return self.__dict__[key]

def prototype(self, dict):
for k in dict:
v = dict[k]
self.__setattr__(k, v)

class Function:
def __init__(): return None

class Class(Prototype, object):
def __call__(self):
return self
def create(self, *args):
return self


class PrototypeExample:
def main(*args):
message = "Hello World!!"

myClass = Class().create()
myClass.prototype({
"message" : "",
"initialize" : (
lambda val :(myClass.__setattr__('message',val))
),
"getMessage" : (
lambda :(myClass.__getattr__('message'))
)
})
myClass.initialize(message)
print(myClass.getMessage())

return 0

if __name__ == "__main__":
try:
PrototypeExample().main()
except Exception, e:
print("Error Exception!!")
print(e)

exit(0)

ただ式にいろいろ書けないのがjsやってる人にはつらいですね・・・

ってamachangが実装してたのか!

http://d.hatena.ne.jp/amachang/20080304/1204633733

なるほど!