SHARE
TWEET

Untitled

a guest Oct 21st, 2019 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Type:
  2.     def __init__(self, name, *, method_table=None, parent=None):
  3.         self.name = name
  4.         self.method_table = method_table or {}
  5.         self.parent = parent
  6.  
  7.     def resolve_method(self, name, /):
  8.         if name in self.method_table:
  9.             return self.method_table[name]
  10.         elif self.parent is not None:
  11.             return self.parent.resolve_method(name)
  12.         return None
  13.  
  14.  
  15. TYPE = Type('type')
  16.  
  17.  
  18. class Method:
  19.     def __init__(self, function, /):
  20.         self.function = function
  21.  
  22.     def call(self, instance, /, args, kwargs):
  23.         return self.function(instance, *args, **kwargs)
  24.  
  25.  
  26. method = Method
  27.  
  28.  
  29. @method
  30. def typeof(instance):
  31.     return instance.base.name
  32.  
  33.  
  34. Object = Type('Object', method_table={'typeof': typeof}, parent=TYPE)
  35.  
  36.  
  37. class Instance:
  38.     def __init__(self, base, args=(), kwargs=None):
  39.         kwargs = kwargs or {}
  40.         self.base = base
  41.         self.fields = {}
  42.  
  43.         if init := self.base.resolve_method('__init__'):
  44.             init.call(self, args, kwargs)
  45.  
  46.     def call_method(self, name, /, args=(), kwargs=None):
  47.         kwargs = kwargs or {}
  48.         if method := self.base.resolve_method(name):
  49.             return method.call(self, args, kwargs)
  50.         raise AttributeError(name)
  51.  
  52.     def getattr(self, name, /):
  53.         if name in self.fields:
  54.             return self.fields[name]
  55.         raise AttributeError(name)
  56.  
  57.     def setattr(self, name, value, /):
  58.         self.fields[name] = value
  59.  
  60.     def delattr(self, name):
  61.         if name in self.fields:
  62.             del self.fields[name]
  63.             return True
  64.         return False
  65.  
  66.  
  67. @method
  68. def get_name(self):
  69.     return self.getattr('name')
  70.  
  71.  
  72. @method
  73. def set_name(self, name, /):
  74.     return self.setattr('name', name)
  75.  
  76.  
  77. @method
  78. def Test_init(self, name):
  79.     self.setattr('name', name)
  80.  
  81.  
  82. Test = Type('Test', method_table={
  83.     'get_name': get_name,
  84.     'set_name': set_name,
  85.     '__init__': Test_init,
  86. }, parent=Object)
  87.  
  88. test = Instance(Test, kwargs={'name': 'hullo'})
  89.  
  90. print('get_name:', test.call_method('get_name'))
  91. print('typeof:', test.call_method('typeof'))
  92. print('set_name', test.call_method('set_name', args=('world',)))
  93. print('get_name:', test.call_method('get_name'))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top