用多个参数链接函数

2024-09-28 22:25:52 发布

您现在位置: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”%>%操作符(管道操作符也存在于Haskell中)。等效代码为:

^{pr2}$

这里只有4个功能,可以有6个,7个可以轻松链接。不幸的是,我不能使用R,我想知道python 2.7中是否有解决方案

这与本主题非常相关,但还有更多的论据: Better way to call a chain of functions in python?


Tags: ofpath方法函数参数os链接def
1条回答
网友
1楼 · 发布于 2024-09-28 22:25:52

fn(see on GitHub>>运算符,还有许多其他函数编程的好东西。您可以使用python进行函数式编程,即使(see also this answer)并不是最佳方法。Python可以通过以下方法“链接”函数:

file = getDbDir(...).makePath(...).createDir(...).getPath(...)

这是连锁经营的一种方式。在

相关问题 更多 >