I'm new to Python. Would you please tell me what's wrong with the following code? When I run it, I got an error message of "NameError: global name 'reduce' is not defined". I asked Goolge but it's useless. :(
def main():
def add(x,y): return x+y
reduce(add, range(1, 11))
if __name__=='__main__':
main()
reduce
was moved out of the built-ins was because it was frequently being used for addition, wheresum
is preferable (in this case, you could just dosum(range(1, 11))
, orsum(xrange(1, 11))
in Python 2). Note also that there is anadd
function equivalent to yours in the standard library:operator.add
. – James