From 27536fea937fc93b78a1dd0a3d5185eb42ae3573 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Fri, 31 Oct 2014 16:38:13 -0400 Subject: use a better reader implementation for most things it goes through the attribute protocol whenever possible --- mop/__init__.py | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/mop/__init__.py b/mop/__init__.py index b2bf41c..dffa4f8 100644 --- a/mop/__init__.py +++ b/mop/__init__.py @@ -121,6 +121,7 @@ def bootstrap(): # Phase 4: manually assemble enough scaffolding to allow object construction + # temporary, we'll have a better version later def gen_reader(name): return lambda self: self.slots[name] @@ -220,6 +221,18 @@ def bootstrap(): # Phase 6: now we can populate the rest of the mop + def get_value(self, instance): + return instance.slots[self.get_name()] + Attribute.add_method(Method.new( + name="get_value", body=get_value + )) + + # here's the better implementation + # note that we can't replace the implementation of the methods implemented + # by the previous gen_reader because that would end up recursive + def gen_reader(name): + return lambda self: self.metaclass.get_all_attributes()[name].get_value(self) + Method.add_method(Method.new( name="get_name", body=gen_reader("name") )) @@ -227,12 +240,6 @@ def bootstrap(): name="get_body", body=gen_reader("body") )) - def get_value(self, instance): - return instance.slots[self.get_name()] - Attribute.add_method(Method.new( - name="get_value", body=get_value - )) - Class.add_attribute(Attribute.new(name="name")) Class.add_attribute(Attribute.new(name="superclass")) Class.add_attribute(Attribute.new(name="attributes", default=lambda: {})) -- cgit v1.2.3-54-g00ecf