Python: jQuery-like function chaining?

前端 未结 5 993
面向向阳花
面向向阳花 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:41

    For future reference: have a look at Moka, a minimalist functional programming library. From their examples:

    (List()                    # Create a new instance of moka.List
       .extend(range(1,20))    # Insert the numbers from 1 to 20
       .keep(lambda x: x > 5)  # Keep only the numbers bigger than 5
       .rem(operator.gt, 7)    # Remove the numbers bigger than 7 using partial application
       .rem(eq=6)              # Remove the number 6 using the 'operator shortcut'
       .map(str)               # Call str on each numbers (Creating a list of string)
       .invoke('zfill', 3)     # Call zfill(x, 3) on each string (Filling some 0 on the left)
       .insert(0, 'I am')      # Insert the string 'I am' at the head of the list
       .join(' '))             # Joining every string of the list and separate them with a space.
    
    >>> 'I am 007'
    

提交回复
热议问题