steve+comp.lang.python at pearwood.info
Fri Nov 22 12:26:07 CET 2013
A frequently missed feature is the ability to chain method calls:
x = 
=> x now equals [3, 2, 1, 4]
This doesn't work with lists, as the methods return None rather than
self. The class needs to be designed with method chaining in mind before
it will work, and most Python classes follow the lead of built-ins like
list and have mutator methods return None rather than self.
Here's a proof-of-concept recipe to adapt any object so that it can be
used for chaining method calls:
def __init__(self, obj):
self.obj = obj
def __getattr__(self, name):
obj = getattr(self.obj, name)
def selfie(*args, **kw):
# Call the method just for side-effects, return self.
_ = obj(*args, **kw)
=> returns [3, 2, 1, 4]
Tested, and works, in CPython 2.4 through 2.7, 3.2 and 3.3, Jython 2.5,
and IronPython 2.6.
See here for further discussion of the limitations:
More information about the Python-list