2

There are many questions related to lambda and printing, but I could not find anything about this exact question.

I'd like to print the results of my lambda function within a print statement. However, I am getting the wrong output. I am using Python 3

from __future__ import print_function
file_name = "tester"
target = "blue"
prediction = "red"

print(file_name,target,prediction, str(lambda x: print('+') if target==prediction else print('-')))

This returns:

tester blue red <function <lambda> at 0x10918c2f0>

How do I get the actual results of the lambda function?

3
  • no need for a lambda, just do print(file_name,target,prediction,'+' if target == prediction else '-') Commented Mar 8, 2015 at 14:21
  • 1
    What's the point of having the lambda? Why but just use the inline if directly? Commented Mar 8, 2015 at 14:21
  • If you are using python 3.x, then why are you doing from __future__ import print_function? Commented Mar 8, 2015 at 14:22

2 Answers 2

7

lambdas are actually functions only. So, unless you invoke them you will not get any result from them. When you do

str(lambda x: print('+') if target==prediction else print('-'))

you are not actually invoking the lambda function, but trying to get the string representation of the function itself. As you can see, the string representation has the information about what the object is and where it is stored in memory.

<function <lambda> at 0x10918c2f0>

Apart from that, the other problem in your lambda is, you are actually invoking print function in it. It will actually print the result but return None. So, even if you invoke the lambda expression, you will get None printed. For example,

>>> print(print("1"))
1
None

But, the good news is, in your case, you don't need lamda at all.

print(file_name, target, prediction, '+' if target == prediction else '-')

The expression,

'+' if target == prediction else '-'

is called conditional expression and will make sure that you will get + if target is equal to prediction, - otherwise.

>>> file_name, target, prediction = "tester", "blue", "red"
>>> print(file_name, target, prediction, '+' if target == prediction else '-')
tester blue red -

Note: If you are using Python 3.x, you don't need to import print_function from the __future__. It already is a function, in Python 3.x.

Python 3.4.0 (default, Apr 11 2014, 13:05:11) 
[GCC 4.8.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> type(print)
<class 'builtin_function_or_method'>
Sign up to request clarification or add additional context in comments.

1 Comment

I actually chose to accept the other answer, even though this is simpler. The other answer is really more appropriate to the overall problem.
-1

just call lambda

print(file_name,target,prediction, (lambda: '+' if target==prediction else '-')())

4 Comments

Thanks! This is actually a more appropriate answer
This is actually a pretty terrible answer; all your lambda is doing is wasting cycles and making the code harder to read than e.g. print(file_name,target,prediction, '+' if target==prediction else '-')
Can you clarify? It seemed like it was more appropriate...?
in this example lambda not necessary, but you can use it, also you can use u"{}{}{}".format(fillename, target, predoction), this is pep solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.