473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Most compact "X if X else Y" idiom

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.
Oct 12 '08 #1
5 3795
On Oct 12, 12:01*am, jbperez...@yahoo.com wrote:
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:
>>(6+ (3<< 1) ) or 'blah'
12
>>(6- (3<< 1) ) or 'blah'
'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'

Oct 12 '08 #2
jb********@yahoo.com wrote:
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"

Oct 12 '08 #3
On Sat, 11 Oct 2008 22:01:46 -0700, jbperez808 wrote:
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).

--
Steven
Oct 12 '08 #4
On Sun, 12 Oct 2008 05:30:33 +0000, Steven D'Aprano wrote:
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.

--
Steven
Oct 12 '08 #5
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.
Oct 12 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Invalid User | last post by:
While trying to print a none empty list, I accidentaly put an "else" statement with a "for" instead of "if". Here is what I had: if ( len(mylist)> 0) : for x,y in mylist: print x,y else:...
11
by: David Morgenthaler | last post by:
How does one overide the iterator implied by the construct "for line in file:"? For example, suppose I have a file containing row,col pairs on each line, and I wish to write a subclass of file...
40
by: Steve Juranich | last post by:
I know that this topic has the potential for blowing up in my face, but I can't help asking. I've been using Python since 1.5.1, so I'm not what you'd call a "n00b". I dutifully evangelize on the...
5
by: Prakash | last post by:
Does anyone know how to compact a table (only) ? I'm using some code o move rows up & down in a continuous form & it works fine, but after some usage the rows refuse to move up or down until I...
81
by: Matt | last post by:
I have 2 questions: 1. strlen returns an unsigned (size_t) quantity. Why is an unsigned value more approprate than a signed value? Why is unsighned value less appropriate? 2. Would there...
28
by: Jess | last post by:
Hello, It is said that if I implement a "swap" member function, then it should never throw any exception. However, if I implement "swap" non- member function, then the restriction doesn't...
4
by: FullBandwidth | last post by:
I have been perusing various blogs and MSDN pages discussing the use of event properties and the EventHandlerList class. I don't believe there's anything special about the EventHandlerList class in...
6
by: .rhavin grobert | last post by:
hello;-) i frequently need the following construction: ReturnParam § Function() § { /...do something.../ someType var § = something; /...do something.../ return something;
5
by: Joe Strout | last post by:
On Nov 13, 2008, at 10:19 AM, Chris Mellon wrote: Argh. I've been back in the Python community for about a month, and I've been continually amazed at how every single "how do I do X" or "what...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.