Say I had a function, like this:
def test(*args, **kwargs):
print(args, kwargs)
And I wanted to call it in a map object, like this:
obj = map(lambda var: test(var+1, var = var), [1, 2, 3])
When I print the values in it, it shows this:
>>> for i in obj:
print(i)
(2,) {'var': 1}
None
(3,) {'var': 2}
None
(4,) {'var': 3}
None
Where are the None
values coming from?
Your mapping function is printing, not returning values, so when you iterate the map
object, i
is being set to None
over and over (None
is returned by any function that doesn't explicitly return
something else). To fix, change your function definition to something like:
def test(*args, **kwargs):
return args, kwargs
so it returns the values rather than printing them, and your loop's print
will see them instead of None
s.
Alternatively, leave the function as is, and change the loop to:
for i in obj:
pass
so you don't print the useless None
s from test. This is generally bad style mind you;
map's mapping function should be side-effect free (
printing is a side-effect) to match the functional nature of
map. In general, functions that
printinstead of
returning are bad ideas; if you
return, the caller can
printif they want to, or use a
returnvalue programmatically, but if you
print`, the caller can't really do much (even if they capture it through cheesy hacks, they're stuck parsing it to do anything useful with it).