Commit 687219d1 authored by Zed's avatar Zed

Enable dynamic method dispatch for attribute get/set.

parent 2a2c566b
......@@ -21,3 +21,4 @@ class Car:
color = self.color
print(f"{who} will drive a {color} {kind} to {where}")
......@@ -12,6 +12,12 @@ def _get(self, attr):
def _set(self, attr, value):
self[attr] = value
def _get_kind(self):
return self['kind']
def _set_kind(self, value):
self['kind'] = value
def eat(self):
set(self, 'fresh', False)
......
......@@ -28,12 +28,24 @@ def new(cls, *args, **kw):
return self
def get(self, attr):
get_m = method(self, 'get')
return get_m(self['data'], attr)
cls = self['class']
get_m = cls.__dict__.get('_get_' + attr)
if get_m:
return get_m(self['data'])
else:
get_m = method(self, 'get')
return get_m(self['data'], attr)
def set(self, attr, value):
set_m = method(self, 'set')
return set_m(self['data'], attr, value)
cls = self['class']
set_m = cls.__dict__.get('_set_' + attr)
if set_m:
return set_m(self['data'], value)
else:
set_m = method(self, 'set')
return set_m(self['data'], attr, value)
def is_a(self, cls):
return self['class'] == cls
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment