Python: jQuery-like function chaining?

前端 未结 5 1009
面向向阳花
面向向阳花 2020-12-30 11:47

I couldn\'t find anything on this subject on Google, so I think I should ask it here:

Is it possible to chain functions with Python, like jQuery does?



        
5条回答
  •  春和景丽
    2020-12-30 12:26

    Take a look at this. It is A simple wrapper class for chaining. And it implemented some of the underscore.js lib's functionality. You wrap your list, tuple or dict with an underscore, and play with it then get the value out of it by appending another underscore.

    print (_([1,2,3])
           .map(lambda x: x+1)
           .reverse()
           .dict_keys(["a", "b", "c"])
           .invert()
           .items()
           .append(("this is what you got", "chaining"))
           .dict()._)
    

    output:

    {2: 'c', 3: 'b', 4: 'a', 'this is what you got': 'chaining'}
    

提交回复
热议问题