How do `any()` and `all()` short-circuit, and where do you use them?
BeginnerConceptLanguage essentials & functions
Answer
any(iterable) returns True at the first truthy item; all(iterable) returns False at the first falsy item. Both stop consuming the iterable early, so with a generator they avoid unnecessary work.
open_to_world = any(r.get("CidrIp") == "0.0.0.0/0" for r in perm["IpRanges"])
all_ready = all(c.status == "True" for c in node.status.conditions if c.type == "Ready")
Note all([]) is True, which can make an empty result look like success.
More on Language essentials & functions
Why is Python the default scripting language in so many DevOps teams, and where would you not use it?BeginnerWhat is the difference between mutable and immutable types? Give examples.BeginnerWhat does assignment do in Python, and how do shallow and deep copies differ?BeginnerExplain the mutable default argument bug and how to fix it.IntermediateWhat is the difference between `==` and `is`?BeginnerWhat values are "falsy" in Python, and why does it matter in scripts?Beginner