Python: jQuery-like function chaining? -


i couldn't find on subject on google, think should ask here:

is possible chain functions python, jquery does?

['my', 'list'].foo1(arg1, arg2).foo2(arg1, arg2).foo3(arg1, arg2) #etc... 

i losing lot of space , readability when write code:

foo3(foo2(foo1(['my', 'list'], arg1, arg2), arg1, arg2), arg1, arg2) #etc... 

there seems exist illusive library creating such functions, can't seem see why has complicated-looking...

thanks!

here's expansion of simon's listmutator suggestion:

class listmutator(object):      def __init__(self, seq):         self.data = seq      def foo1(self, arg1, arg2):         self.data = [x + arg1 x in self.data]         # allows chaining:         return self      def foo2(self, arg1, arg2):         self.data = [x*arg1 x in self.data]         return self  if __name__ == "__main__":     lm = listmutator([1,2,3,4])     lm.foo1(2, 0).foo2(10, 0)     print lm.data      # or, if must:     print listmutator([1,2,3,4]).foo1(2, 0).foo2(10, 0).data 

you go 1 better , make listmutator act entirely list using collections abstract base classes. in fact, subclass list itself, although may restrict doing things might need do... , don't know general opinion on subclassing built-in types list.


Comments

Popular posts from this blog

Add email recipient to all new Trac tickets -

400 Bad Request on Apache/PHP AddHandler wrapper -

php - Change action and image src url's with jQuery -