A library approach to the ternary operator

Brandon Beck bbeck at NOSPAM.austin.rr.com
Fri Mar 21 17:47:55 EST 2003


> My choice would be:
> 
> # ternary module
> def select(test, fn_iftrue, fn_iffalse):
>     if test:
>         return fn_iftrue()
>     return fn_iffalse()
> 
> then:
> 
> from ternary import *
> x = select(condition, lambda:true_result, lambda:false_result)


Another possibility along these sames lines is:

# ternary module
def select(test, iftrue, iffalse):
	if test:
		if callable(iftrue):
			return iftrue()
		else:
			return iftrue
	else:
		if callable(iffalse):
			return iffalse()
		else:
			return iffalse()

then the user can use either the short circuiting or non-short 
circuiting version depending on what they want and what's more
appropriate/readable.

Both of these then work:
	x = select(condition, lambda:true_result, lambda:false_result)
	x = select(condition, true_result, false_result)



Brandon





More information about the Python-list mailing list