3

I am trying to use lambda for a usecase, but it seems not working. The following is what I tried

The below is what I used, in stackflow I came across many people getting output for this, but for me its None

from __future__ import print_function
f = lambda x: print(x)
f()
f("Hello") ### now out put is shown expected is Hello output
f("Hello") is None ## I checked if its None and it is
>>> True 

enter image description here then I tried something else

l = lambda x : sys.stdout.write(x)
l('hello') # this time its showing output as 5 instead of printing hello
>>> 5

enter image description here

I even tried something which is not using lambda, unfourtunately that too not working, and got None

from functools import partial
spam = partial(print, 'Hello!')
spam()
spam() is None
>>> True

I got this from the last answer in this question assigning print function to variable

Can anyone help me what I am missing and why I am not able to print the string?

11
  • This is really amazing! Commented Nov 10, 2018 at 13:23
  • 1
    With Python3 l('hello') is outputting hello and returning 5 on my system. With python2 I only get the hello output without the integer return value. Commented Nov 10, 2018 at 13:27
  • Please consider telling a bit more about the environment you are using. Also, if you just write a simple print("Hello"), does it appear correctly? Commented Nov 10, 2018 at 13:42
  • @tevemadar Yes print("Hello") prints Hello. I am using inside jupyter notebook Commented Nov 10, 2018 at 13:45
  • Are you perhaps supressing error messages somehow? If I put the first snippet into a single cell, the empty f() call raises TypeError for me both locally and on jupyter.org/try. Then the rest of the lines does not run, but the error message is pretty visible by default. Have you tried restarting the kernel? Based on your screenshot, you are at the 59-60th input, and who knows happened earlier. Commented Nov 10, 2018 at 13:54

2 Answers 2

2

What's not expected? that f('Hello') is None evaluate to True? Well that's expected because it is None.

lambda x: print(x) has no 'return', only a side effect, and the side effect is to print stuff to the output. This is similar to a function

def func(x): 
    print(x)

which has no return statement, hence, if you evaluate the function, it's output will be None.

Sign up to request clarification or add additional context in comments.

13 Comments

@ Rocky Li, this is about when you call l = lambda x : sys.stdout.write(x) and then call it like l('hello') it returns hello5 rather just hello !
Its not printing output. Say if I type f("Hello") it prints nothing. And in other case l('hello') prints the number of count of characters not string
@pygo: l('hello') prints hello (if flushed) and returns 5, since sys.stdout.write returns the number of characters written, see docs.python.org/3/library/io.html#io.TextIOBase.write
@pygo How you getting the hello5 ? I am just getting 5
@pygo haha this is funky. Now I am getting something like you , with the hello5. When tried in shell
|
2
l = lambda x : sys.stdout.write(x)
l('hello') # this time its showing output as 5 instead of printing hello
>>> 5

it's probably because the write is not flushed, you can force this by manually calling

sys.stdout.flush()

As for why it's returns hello5 is because the Python shell prints out the return value of the previous statement, sys.stdout.write() returns the count of the written characters which in case of 'hello' is 5. thus they become concatenated into hello5. If you ran this in a normal Python file i.e python filename.py it would never print the 5 and only hello.

5 Comments

But how to print just "Hello" instead of the number of characters ?
Your code should work perfectly fine outside of the Python interpreter REPL (put it in a separate file and try)
@UkuLoskit I put this inside a python file and still it returning the number.l = lambda x : sys.stdout.write(x); sys.stdout.flush(). l(hello).. returned 5
@UkuLoskit, sounds interesting though , you are right outside interpreter while keeping into a file then works as expected and do not return a number!
@gboffi, thanks, I just realized this as well, my mistake :)

Your Answer

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