Most compact "X if X else Y" idiom

jbperez808 at yahoo.com jbperez808 at yahoo.com
Sun Oct 12 07:01:46 CEST 2008


I find myself having to do the following:

  x = (some complex expression)
  y = x if x else "blah"

and I was wondering if there is any built-in idiom that
can remove the need to put (some complex expression)
in the temporary variable x.

e.g. something like the below:

 y= foobar ((some complex expression), "blah")

I realized foobar() can be easily coded as:
  def foobar(a,b):
    if a: return a
    else: return b

But I was wondering if there was a built-in function or syntax
that already does this.



More information about the Python-list mailing list