Method chaining

Wolfgang Maier wolfgang.maier at biologie.uni-freiburg.de
Fri Nov 22 12:55:45 EST 2013


Wolfgang Maier <wolfgang.maier <at> biologie.uni-freiburg.de> writes:

> 
> Steven D'Aprano <steve+comp.lang.python <at> pearwood.info> writes:
> 
> > # With chaining
> > thing = func(MyClass().spam().eggs().cheese(), 
> >              MyClass().aardvark(),
> >              OtherClass().fe().fi().fo().fum(),
> >              )
> > do_stuff_with(thing)
> > 
> > versus:
> > 
> > # Without chaining
> > temp1 = MyClass()
> > temp1.spam()
> > temp1.eggs()
> > temp1.cheese()
> > temp2 = MyClass()
> > temp2.aardvark()
> > temp3 = OtherClass()
> > temp3.fe()
> > temp3.fi()
> > temp3.fo()
> > temp3.fum()
> > thing = func(temp1, temp2, temp3)
> > do_stuff_with(thing)
> > 
> 
> Another use case might be in comprehensions and generator expressions ??
> 
> thing = [MyClass().spam().eggs(x).cheese() for x in sequence]
> 
> where you can't use all those temporary assignments.
> 
> Best,
> Wolfgang
> 
> 

Thinking about this, you could define __call__ for your chained class to
return the wrapped object:

class chained:
    def __init__(self, obj):
        self.obj = obj
    def __repr__(self):
        return repr(self.obj)
    def __getattr__(self, name):
        obj = getattr(self.obj, name)
        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
    def __call__(self):
        return self.obj

This would encourage its localized use on the fly as in:

thing = [MyClass().spam().eggs(x).cheese()() for x in sequence]

where the intention probably would be to generate a list of lists (or other
mutable objects), not of objects of class chained. Localizing the use of
chained also seems like a good idea to me as I do share Peter's worries
about coding style, while I also agree with you that such a class comes in
handy from time to time.





More information about the Python-list mailing list