any
can take a generator253 if not guild_id:
254 return False
255
256 valid_premium = any(257 [
258 entitlement.is_expired() is False and entitlement.guild_id == guild_id
259 async for entitlement in self.entitlements()
The built-in function being used does not require comprehension and can work directly with a generator expression.
Using a generator expression within these functions is faster than using a comprehension:
all
any
enumerate
iter
itertools.cycle
itertools.accumulate
The inbuilt functions all()
and any()
in Python also support short-circuiting (evaluation stops as soon as the overall return value of the function is known), but this behavior is lost if you use a list comprehension. This affects performance.
# Takes a very long time to run
all_evens = all([n % 2 == 0 for n in range(100000000)])
if all_evens:
print('All numbers are even!')
# No list comprehension, runs instantly as `1` is found to be not even
all_evens = all(n % 2 == 0 for n in range(100000000))
if all_evens:
print('The numbers are all even!')