在python中使用部分函数应用程序
pypartial的Python项目详细描述
pypartial是一个简单的python库,允许在本地python中使用部分函数应用程序符号。它在试图编写简洁明了的代码时非常有用,并扩展了python的函数式编程特性所提供的易用性。
示例:
import pypartial #use <_ notation to partial apply functions
binary = int<_(base=2) #you can pass keyword arguemnts print(binary(‘101010100101’))
import random dice=random.randint<_(1, 6) #you can also pass non-keywords print(dice()) print(dice())
up_to_ten = range<_(_, 11) #you can leave args blank with ‘_’ print(list(up_to_ten(5)))
import operator double = operator.mul<_(2) print(double(7))
- class Foo(object):
- def bar(self, a, b):
- print([a,b])
- def egg(self, a, b, c=9):
- print([a,b,c])
foo=Foo() bar7 = foo.bar<_(_, b=7) #works with methods too bar7(2)
egg1 = foo.egg<_(_, 5) egg2 = egg1<_(c=9) #you can partially apply multiple times egg2(6)
print(egg2) #nice string representation