我的教授错了吗?
Is my prof wrong?
我正在解决一个问题,但有些事情让我感到困惑。在下面的代码中不是
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
完全正确,它们被称为命名参数。它们是一种在函数 all 中间跳过任意参数的方法,并且仍然知道您实际发送了哪些参数。
这种模式实际上非常普遍,从 VB6 到 C# 等等!
在 python 函数中,classes 是第一个 class 公民,您可以像任何其他变量一样传递它们
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
我正在解决一个问题,但有些事情让我感到困惑。在下面的代码中不是
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
完全正确,它们被称为命名参数。它们是一种在函数 all 中间跳过任意参数的方法,并且仍然知道您实际发送了哪些参数。
这种模式实际上非常普遍,从 VB6 到 C# 等等!
在 python 函数中,classes 是第一个 class 公民,您可以像任何其他变量一样传递它们
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