aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2014-11-01 00:33:59 -0400
committerJesse Luehrs <doy@tozt.net>2014-11-01 00:33:59 -0400
commit6954fa2b819feebf8c0e862a165f53aa5ecc3839 (patch)
tree8908400b94b6343e9b1871f876528f953abaa4dc
parent483c36526b1b70a6c0d20433ce0ad2699e52e9b5 (diff)
downloadpython-mop-6954fa2b819feebf8c0e862a165f53aa5ecc3839.tar.gz
python-mop-6954fa2b819feebf8c0e862a165f53aa5ecc3839.zip
make the constructor go through the attribute protocol as well
-rw-r--r--mop/__init__.py31
1 files changed, 16 insertions, 15 deletions
diff --git a/mop/__init__.py b/mop/__init__.py
index 296c013..ac634ae 100644
--- a/mop/__init__.py
+++ b/mop/__init__.py
@@ -171,18 +171,29 @@ def bootstrap():
"get_default_for_instance", get_default_for_instance
))
+ # set_value requires get_name
+ Attribute.add_method(bootstrap_create_method(
+ "get_name", gen_reader("name")
+ ))
+
+ # create_instance requires set_value
+ def set_value(self, instance, new_value):
+ instance.slots[self.get_name()] = new_value
+ Attribute.add_method(bootstrap_create_method(
+ name="set_value", body=set_value
+ ))
+
# new requires create_instance
def create_instance(self, kwargs):
- slots = {}
+ instance = BasicInstance(self, {})
+ instance.__class__ = python_class_for(self)
attrs = self.get_all_attributes()
for attr_name in attrs:
attr = attrs[attr_name]
if attr_name in kwargs.keys():
- slots[attr_name] = kwargs[attr_name]
+ attr.set_value(instance, kwargs[attr_name])
else:
- slots[attr_name] = attr.get_default_for_instance()
- instance = BasicInstance(self, slots)
- instance.__class__ = python_class_for(self)
+ attr.set_value(instance, attr.get_default_for_instance())
return instance
Class.add_method(bootstrap_create_method(
"create_instance", create_instance
@@ -196,10 +207,6 @@ def bootstrap():
# Phase 5: Object construction works, just need attributes to construct with
- Attribute.add_method(bootstrap_create_method(
- "get_name", gen_reader("name")
- ))
-
def add_attribute(self, attr):
self.get_local_attributes()[attr.get_name()] = attr
Class.add_method(bootstrap_create_method(
@@ -227,12 +234,6 @@ def bootstrap():
name="get_value", body=get_value
))
- def set_value(self, instance, new_value):
- instance.slots[self.get_name()] = new_value
- Attribute.add_method(Method.new(
- name="set_value", body=set_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