Yes, it is very specific to ChainMap
. But I am just using this example to look into performance of “predicated short-circuiting”.
It is not the first time I am replacing any
with for loop
. This becomes highly preferable for sequences of small sizes (up to 50 elements).
Just to give another example. Graph building. There is a node, which contains other nodes of different types. It is often needed to check if all sub-nodes are of the same type.
class Node:
def __init__(self, func, args):
self.func = func
self.args = args
...
if all(isinstance(a, Atom) for a in node.args):
#do something simple
....
Same as in case of a ChainMap
, it has significant impact when such operation is done many times. Every time __contains__
is called in case of ChainMap
and recursive operations on graphs in above example.