I'm using Python 3.2. Tried this:
xor = lambda x,y: (x+y)%2
l = reduce(xor, [1,2,3,4])
And got the following error:
l = reduce(xor, [1,2,3,4])
NameError: name 'reduce' is not defined
Tried printing reduce
into interactive console - got this error:
NameError: name 'reduce' is not defined
Is reduce
really removed in Python 3.2? If that's the case, what's the alternative?
The Python "NameError: name is not defined" occurs when we try to access a variable or function that is not defined or before it is defined. To solve the error, make sure you haven't misspelled the variable's name and access it after it has been declared.
The Python "NameError: name 'reduce' is not defined" occurs when we use the reduce() function without importing it first. To solve the error, import the function from the functools module before using it - from functools import reduce . Here is an example of how the error occurs. Copied!
Python's reduce() is a function that implements a mathematical technique called folding or reduction. reduce() is useful when you need to apply a function to an iterable and reduce it to a single cumulative value.
To specifically handle NameError in Python, you need to mention it in the except statement. In the following example code, if only the NameError is raised in the try block then an error message will be printed on the console.
It was moved to functools
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With