Marko Rauhamaa wrote: > "BartC" <b...@freeuk.com>: > >>> Ah, but what would >>> >>> x = [f, g][cond]() >>> >>> produce? >> >> It will select f or g (which should refer to functions), and call one of >> those depending on cond. That's not a problem. >> >> The problem is it will still evaluate both f and g, > > That's not really the problem. The problem is in readability.
I don't get why that's considered hard to read. We write things like this all the time: item = somelist[index] value = data[key] Presumably people won't have a problem with: values = [f(), g(), h()] value = values[index] (If they do, they're going to have a bad time with Python.) They probably won't even mind if we skip the temporary variable: value = [f(), g(), h()][index] and if they're experienced with languages that treat functions as first-class values, they'll be fine with factoring out the function call: value = [f, g, h][index]() So why is it hard to read when the index is a flag? value = [f, g][cond]() Of course one can write hard-to-read code using any idiom by sheer weight of complexity or obfuscated naming: value = [some_function(arg)[23]['key'] or another_function.method((x + y)/(z-x**(y-4)))* some_list[get_index(a)].spam(eggs=False, tomato='yum'), something.do_this(p|q).get(alpha, beta) ^ aardvark.bobble("string%s" % carrot.gamma(r&s)* (this & that).fetch(83, 36, when=when or "now") ][cond or flag or foo(42)-1 > 17 or bar(b) < thingy(c) or not d] but re-writing that using ternary if operator won't help one iota. I don't see why `[a, b][flag]` is inherently less readable than `b if flag else a`. -- Steven -- https://mail.python.org/mailman/listinfo/python-list