def _delegate_or(smethname, func):
def delegated(self, *a, **kwa):<EOL><INDENT>wrapped = object.__getattribute__(self, '<STR_LIT>')<EOL>mutator = object.__getattribute__(self, '<STR_LIT>')<EOL>wrapped = mutator(wrapped)<EOL>object.__setattr__(self, '<STR_LIT>', wrapped)<EOL>try:<EOL><INDENT>meth = getattr(wrapped, smethname)<EOL><DEDENT>except AttributeError:<EOL><INDENT>return func(wrapped, *a, **kwa)<EOL><DEDENT>if not isinstance(wrapped, type):<EOL><INDENT>return meth(*a, **kwa)<EOL><DEDENT>else:<EOL><INDENT>meth = getattr(type(wrapped), smethname)<EOL>return meth(wrapped, *a, **kwa)<EOL><DEDENT><DEDENT>return delegated<EOL>
Produce a method that calls either the existing method of the wrapped
object with the same name or a special invocation if it's missing
(see SPECIAL_METHODS in special_methods.py).

Usage: OnAccessMutant(initial_object, callable_mutator)
where: callable_mutator(wrapped_object) -> new_wrapped_object