(a==b) ? 'Yes' : 'No'

Steve Howell showell30 at yahoo.com
Sat Apr 3 05:18:59 CEST 2010


On Apr 2, 5:53 pm, Steven D'Aprano <st... at REMOVE-THIS-
cybersource.com.au> wrote:
>
> As I've pointed out before, it is natural syntax in English. Not
> necessarily the most common, but common enough to be completely
> unexceptional:
>
> "I'll be there in ten minutes, if I can find a parking space close by,
> otherwise you should start without me."
>

To Steven's example, the ternary statement is a nice idiom when it
emphasizes the most common results:

  wait_time = 10 if parking_space_close_by else
expected_wait_time_in_congested_area()

Or:

  qoutient = m / n if n else None

In languages like Ruby/Perl the inverted if statement is also a useful
idiom to emphasize concisely that code is exceptional in nature:

def quotient(m, n)
 # guard code
 return None if n == 0

 # happy path
 return m / n
end


Or:

 raise 'Armegeddon' if locusts_flying()
 useful_intelligible_happy_path_code_here()






More information about the Python-list mailing list