1

Assume you have a function, that sometimes returns a value, and sometimes doesn't, because there really is nothing you could return in this case, not even a default value or something. Now you want to do something with the result, but of course only when there is one.

Example:

result = function_call(params)
if result:
    print result

Is there a way to write this in a more pythonic way, maybe even in one line?

Like that:

print function_call(params) or #nothing

(Note that I mean it shouldn't print "nothing" or "None". It should actually just not print at all, if the result is None)

5
  • 1
    A requirement is no setup, I assume? If setup code is OK, you could make a function for that. Especially easy to integrate in py3 where print is a function itself. Commented Aug 6, 2012 at 11:02
  • Isn't it 3 lines of Python-like code? We can usually replace 3 lines of Java code with about 1/3 of a line of Python :) Commented Aug 6, 2012 at 11:58
  • Yes @gnibbler, if something: lines are a big part of what is very Java-like and can be often written better in python. Commented Aug 6, 2012 at 11:59
  • 1
    @erikb85, porting just 3 lines at a time doesn't give much chance to leverage the power of Python. Commented Aug 6, 2012 at 12:07
  • @gnibbler: I am not porting. And yes, I just want to improve this little detail in my skillset. I do that often, so my python code improves step by step over the years. :) Commented Aug 6, 2012 at 12:09

3 Answers 3

5

No; in Python, name binding is a statement and so cannot be used as an expression within a statement. Since print is also a statement you're going to require 3 lines; in Python 3 you could write:

result = function_call(params)
print(result) if result else None

This isn't quite true for name binding within a comprehension or generator, where name binding is a syntax item that has statement-like semantics:

[print(result) for result in generator_call(params) if result]

As Kos says, you can abuse this to create a one-element comprehension:

[print(result) for result in (function_call(params), ) if result]

Another syntax item that performs name binding and can similarly be abused is the lambda expression:

(lambda result: print(result) if result else None)(function_call(params))

Note that in both these cases the operation on the return value must be an expression and not a statement.

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

4 Comments

+1; you can make that for result in (function_call(params),).
That's more code golf than pythonic refactoring though. The 3 line version is significantly more readable.
At first I thought print(result) if result else None would print None, but I was wrong. It's actually working!
@erikb85, that's because the first part (print(result)) is only evaluated if the check condition (if result) evaluates to True. Otherwise None is evaluated - which, obviously evaluates to None :)
3

I think the more Pythonic version is actually closer to your original:

result = function_call(params)
if result is not None:
    do_something(result)

Checking for is (not) None seems very idiomatic to me - I've used it several times myself and I've also seen it used elsewhere[citation-needed].

3 Comments

Depends if you want to do_something is result is an empty string/list/etc. or not. +1 for just sticking with the 3 lines
This is very expressive in terms of "what happens here", but not in terms of "what did the author want to do here". It's a completely valid response, yet just not my personal taste for python code.
@erikb85, as I mentioned, I believe the Pythonic standard is to return None when there's no viable result (see dict.get for one example).
-1

From the answers up to now I would do that:

>>> from __future__ import print_function #if Python2.7
>>> def filtered_print(txt):
...    txt and print(txt)
...
>>> filtered_print('hello world')
hello world
>>> filtered_print('None')
None
>>> filtered_print(None)
>>>

If someone else has a better solution in mind, I am still open for alternatives, though!

2 Comments

Note that someone downvoted this, because he thinks, it is bad style to write code in A and B style instead of 2 lines with an if. I think it's not dangerous and a lot of people will understand this code quite fine. Depending on the people you work with you might choose not to use this solution!
Finally I decided against this, because the marked solution works and looks better.

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.