Robert Brewer wrote:
> So currently, all subclasses just override __set__, which leads to a
> *lot* of duplication of code. If I could write the base class' __set__
> to call "macros" like this:
>
> def __set__(self, unit, value):
> self.begin()
> if self.coerce:
> value = self.coerce(unit, value)
> oldvalue = unit._properties[self.key]
> if oldvalue != value:
> self.pre()
> unit._properties[self.key] = value
> self.post()
> self.end()
>
> defmacro begin:
> pass
>
> defmacro pre:
> pass
>
> defmacro post:
> pass
>
> defmacro end:
> pass
Here is a way to write that using anonymous blocks:
def __set__(self, unit, value):
with self.setting(unit, value):
if self.coerce:
value = self.coerce(unit, value)
oldvalue = unit._properties[self.key]
if oldvalue != value:
with self.changing(oldvalue, value):
unit._properties[self.key] = value
def setting(self, unit, value):
# begin code goes here
yield None
# end code goes here
def changing(self, oldvalue, newvalue):
# pre code goes here
yield None
# post code goes here
> ...(which would require macro-blocks which were decidedly *not*
> anonymous) then I could more cleanly write a subclass with additional
> "macro" methods:
>
> defmacro pre:
> old_children = self.children()
>
> defmacro post:
> for child in self.children:
> if child not in old_children:
> notify_somebody("New child %s" % child)
def changing(self, oldvalue, newvalue):
old_children = self.children()
yield None
for child in self.children:
if child not in old_children:
notify_somebody("New child %s" % child)
Which do you prefer? I like fewer methods. ;-)
Shane
_______________________________________________
Python-Dev mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe:
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com