有没有一种方法可以链接多个参数的函数?在
目前,我的操作是这样的:
def createDirIfNecessary(directoryName):
if not os.path.exists(directoryName):
print 'Creating directory [%s]'% directoryName
os.makedirs(directoryName)
return directoryName
cakeName = 'lemonPie'
cookDate = '2011-01-04'
#yewww very ugly, big blob of function call ...
myDir = os.path.join(getDbDir('kitchenCupboardDir'),'cakes', cakeName)
file = os.path.join(createDirIfNecessary(myDir), cookDate + '.gz')
例如,在R
中,有一种非常优雅的方法可以使用“pipe”%>%
操作符(管道操作符也存在于Haskell
中)。等效代码为:
这里只有4个功能,可以有6个,7个可以轻松链接。不幸的是,我不能使用R,我想知道python 2.7
中是否有解决方案
这与本主题非常相关,但还有更多的论据: Better way to call a chain of functions in python?
fn
包(see on GitHub有>>
运算符,还有许多其他函数编程的好东西。您可以使用python进行函数式编程,即使(see also this answer)并不是最佳方法。Python可以通过以下方法“链接”函数:这是连锁经营的一种方式。在
相关问题 更多 >
编程相关推荐