Most compact "X if X else Y" idiom

J

jbperez808

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.
 
A

Aaron \Castironpi\ Brady

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.

You could take your chances on 'or', as follows:
'blah'

You don't need to use the ternary statement:

y = (some complex expression)
if not y:
y = "blah"

If you find yourself using it a lot, why not add it to your site's
utilities modules? Take your time, and if you find numerous uses,
present them and make the case Python should have a built-in to do it,
something like 'ditto' marks:

(6- (3<< 1) ) if ditto else 'blah'
 
T

Terry Reedy

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.

A common idiom for this particular case where the if-expression is also
the conditional or the basic of the conditional expression is

y = <some complex expression>
if not y: y = "blah"
 
S

Steven D'Aprano

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.

Use short-circuit Booleans:

y = x or "blah"

If x is any true value (non-zero number, non-empty string etc.) then y
will be set to x; but if x is any false value (zero, empty string, None,
empty list, etc.) then y will be set to "blah".


However, this technique doesn't work for arbitrary tests. For example,
you can't simplify the following:

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

(at least I can't think of any way).
 
S

Steven D'Aprano

Use short-circuit Booleans:

y = x or "blah"

Except of course you don't use x, you use the complex expression.

y = (some complex expression) or "blah"



Sorry for the itchy posting finger.
 
J

jbperez808

Thanks, folks.

Short-circuit boolean was the syntax I had in mind which
momentarily escaped me, but the "if not x: x='blah'" idiom
was instructive as well.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,123
Latest member
Layne6498
Top