我的教授错了吗?

时间:2015-04-20 19:26:37

标签: python mapreduce

我解决了一个问题而且让我感到困惑。在以下代码中没有

counts = mapReduce(lines, mapper=computeWordCounts, reducer=sumUpWordCounts);

错了?这仅仅是伪代码还是这种用法实际上是可能的?

def computeWordCounts(line):
    # TODO

def sumUpWordCounts(word, counts):
    # TODO

def mapReduce(data, mapper, reducer):
    # TODO

def test():
    with open('/Users/bgedik/Desktop/zzz.txt') as f:
        lines  = f.read().splitlines()
    counts = mapReduce(lines, mapper=computeWordCounts, reducer=sumUpWordCounts);
    for word, count in counts:
        print word, " => ", count

2 个答案:

答案 0 :(得分:3)

在python函数和类中是一等公民,你可以像传统的其他变量一样传递它们

def square(a_var):
    return a_var ** 2

def apply(value,fn):
   return fn(value)

print apply(5,square)

您也可以重命名

sq = square
print sq(5)

还有更多内容,请参阅文档https://docs.python.org/2/library/stdtypes.html#functions

答案 1 :(得分:-2)

它完全有效,它们被称为命名参数。它们是在函数中间跳过任意参数的一种方式,并且仍然知道你实际发送了哪些参数。

这种模式实际上非常普遍,从VB6到C#等等!

相关问题