Python lambda function printing <function <lambda> at 0x7fcbbc740668> instead of value
You aren't calling the function. It's the same as if you wrote
print convert_ascii
instead of print convert_ascii(i)
.
Try
print (lambda x: chr(ord(x) + 1))(i)
Note that I changed ord(i)
to ord(x)
in the function body.
Currently you are printing a function object. You have to call the function.
Receive the function in a variable and call it with a parameter.
for i in word:
print convert_ascii(i)
fun=lambda x: chr(ord(x) + 1)
print fun(some_arg)
The Lambda Keyword returns an anonymous function:
>>> func = lambda x: x+1
>>> print(func)
<function <lambda> at 0x7f0310160668>
the above is (not counting the behind-the-scenes magic) equivalent to:
>>> def func(x):
return x+1
>>> print(func)
<function func at 0x7fa73d3e6bf8>
to invoke the function, lambda or not, you still have to call it:
>>> print(func)
<function <lambda> at 0x7f0310160668>
>>> func(123)
124
That said, Lambdas are not very well suited to this situation, and are better used if a function or construct requires a short function.
>>> word = "spam"
>>> map(lambda x: chr(ord(x) + 1), word)