Python 具有多个参数的链接函数
有没有一种方法可以链接具有多个参数的函数 目前,我在Python 具有多个参数的链接函数,python,r,python-2.7,functional-programming,chain,Python,R,Python 2.7,Functional Programming,Chain,有没有一种方法可以链接具有多个参数的函数 目前,我在Python中以如下方式“链接”我的操作: def createDirIfNecessary(directoryName): if not os.path.exists(directoryName): print 'Creating directory [%s]'% directoryName os.makedirs(directoryName) return directoryName cak
Python
中以如下方式“链接”我的操作:
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”%%>%%
操作符(pipe操作符也出现在Haskell
中)。等效代码为:
cakeName = 'lemonPie'
cookDate = '2011-01-04'
file = getDbDir('kitchenCupboardDir') %>%
file.path('cakes', cakeName) %>%
createDirIfNecessary %>%
file.path(paste0(cookDate,'.gz'))
这里只有4个函数,可以有6个,7个,可以轻松链接。不幸的是,我不能使用R,我想知道在Python2.7中是否有解决方案
这与本主题非常相关,但有进一步的论据:
fn
包中有>
操作符和许多其他函数编程的好东西。您可以用python进行函数式编程,即使()这并不是最佳方式。Python可以通过以下方法“链接”函数:
file = getDbDir(...).makePath(...).createDir(...).getPath(...)
这是连锁经营的一种方式