I am using Python3.6 and am trying to figure out ways in which we can use map
with multiple arguments.
When I run,
def multiply(x, y, z):
return x * y * z
products = map(multiply, [3,6], [1,8], [3,5])
list(products)
returns [9, 240]
as expected
However, when I specify a default value for z,
def multiply(x, y, z = [3,5]):
return x * y * z
products = map(multiply, [3,6], [1,8])
list(product)
returns
[[3, 5, 3, 5, 3, 5],
[3,
5,
3,
5,
3,
5,
3,
5,
3,
5,
3,
5,
3,
5,
3,
5,
3,
5,
3,
5,
3,
5,
3,
5,
3,
5,
3,
5,
3,
5,
3,
5,
3,
5,
3,
5,
3,
5,
3,
5,
3,
5,
3,
5,
3,
5,
3,
5,
3,
5,
3,
5,
3,
5,
3,
5,
3,
5,
3,
5,
3,
5,
3,
5,
3,
5,
3,
5,
3,
5,
3,
5,
3,
5,
3,
5,
3,
5,
3,
5,
3,
5,
3,
5,
3,
5,
3,
5,
3,
5,
3,
5,
3,
5,
3,
5]]
Why does Python differ in the way it runs map
in the two scenarios?
When you set the default value of z during execution with map this happens:
3 * 1 * [3,5]
6 * 8 * [3,5]
Therefore your output, for map to work as you intend you should explicitly give the list as one of it arguments and in your case the default value of z is not a direct argument of map. Hope this makes sense.