Deprecated: Assigning the return value of new by reference is deprecated in /f2/blendedtechnologies/public/wp-content/plugins/pmetrics.php on line 1192
Blended Technologies » Blog Archive » VB’s iif in Python

VB’s iif in Python

Here you go:

def iif(condition,resultiftrue,resultiffalse):
    if condition:return resultiftrue
    else: return resultiffalse

#usage: furrycoat=iif(is_summer==True,”light coat”,”Heavy coat”)

It’s very simple I know, probably not worthy of posting, however a search for “python iif” returned nothing useful, and some misleading answers. And my philosophy is to post anything I search for and the answer isn’t readily available.

del.icio.us |  Digg |  FURL |  Yahoo! My Web 2.0 |  Reddit

10 Responses to “VB’s iif in Python”

  1. bitprophet Says:

    I’ve taken to using an even simpler, if not quite as easy to follow, construct:

    value = boolean and truevalue or falsevalue

    So, in your example:

    furrycoat = is_summer and “light coat” or “Heavy coat”

  2. Administrator Says:

    furrycoat = is_summer and “light coat” or “Heavy coat”

    That looks like a good idea. I’m not sure why it works offhand. The benefit to that it should short-circuit, whereas my iif evaluates all of the arguments. I’ll give that a try.

  3. bitprophet Says:

    It appears to work because it’s just simple logic; I tested it extensively when I first came up with it and it does appear to work exactly as you’d expect. Probably a case of “naw, that’s too simple, it can’t work!”.

    I had been looking for an analogy to the PHP/C ‘value = boolean ? truevalue : falsevalue;’ which is yet another way of expressing the same thing.

  4. Bob Ippolito Says:

    It’s not the same thing. “a ? b : c” or “iif(a, b, c)” will return b if a is true and b is not true. “a and b or c” will only return b if both a and b are true. This is a common mistake for people trying to get clever with Python syntax. You could use “(a and [b] or [c])[0]” or simply write it out:

    if a:
    val = b
    else:
    val = c

  5. bitprophet Says:

    Figures :(

  6. mm Says:

    just for fun..
    tuple/array-assignment
    #int(True)==1,int(False)==0

    furrycoat = (’light coat’,'heavy coat’)[int(is_summer)]

  7. Administrator Says:

    tuple/array-assignment
    #int(True)==1,int(False)==0
    furrycoat = (’light coat’,’heavy coat’)[int(is_summer)]

    This looks promising.

  8. SJB Says:

    Please consider readability. Some code is just one-off that never gets read by another person - like in the interactive python shell. If you want to save a couple line breaks in such a rare situation, by all means use your method.

    However this conditional assignment has been rejected for inclusion in python proper partly because it is so unreadable.

    if is_summer:
    furrycoat = ‘light coat’
    else:
    furrycoat = ‘heavy coat’

    Is, IMHO, the best way to do it.

  9. GC Says:

    Why don’t they just include the C-style ?: expressions? Everyone can read them, you save the linebreaks for simple expressions, it’s win-win!

  10. fnl Says:

    @mm: wrong!

    (’heavy coat’, ‘light coat’)[int(is_summer)]

    but thanks for the tip…