So it’s idiomatic Python to use containers rather than extended Boolean expressions or if/else or switch statements, e.g. this:
if key in ('foo', 'bar'): ...
Rather than:
if key == 'foo' or key == 'bar': ...
Or:
keys = { 'foo': 1, 'bar': 2 }
value = keys[key]
Rather than:
if key = 'foo': value = 1
elif key = 'bar': value = 2
But what’s the performance cost of the idiomatic approaches though? If these statements were in a tight loop for dealing with large amounts of data or 3d rendering, would it be better to avoid the idiomatic forms?
The long-winded Boolean expressions might cost more since it’s interpreted, but that would have to be a long set of conjunctions before it would be larger than the cost of instantiating a tuple. Same for the dict construction and lookup.
I assume that Python needs to evaluate and construct the container every time its definition is executed, but does CPython do any optimisation around that?
Any thoughts?