On Fri, Nov 22, 2013 at 10:26 PM, Steven D'Aprano
<[email protected]> wrote:
> if callable(obj):
> def selfie(*args, **kw):
> # Call the method just for side-effects, return self.
> _ = obj(*args, **kw)
> return self
> return selfie
> else:
> return obj
Nice piece of magic. One limitation not mentioned is that this
completely destroys the chance to have a method return anything _other
than_ self. Since this is intended for Python's convention of
"mutators return None", I'd be inclined to check for a None return,
though that might still have some false positives.
def selfie(*args, **kw):
# Call the method for side-effects, return self if it
returns None.
_ = obj(*args, **kw)
if _ is None: return self
return _
return selfie
Either that, or manually identify a set of methods to wrap, which
could possibly be done fairly cleanly with a list of names passed to
__init__. That'd be more work, though.
ChrisA
--
https://mail.python.org/mailman/listinfo/python-list