我想使用all(map(func, iterables))
,因爲很明顯,但我對這種方法是否優化非常感興趣?例如,如果任何計算結果map()
不是True
映射應該停止。從我的項目Python中是否存在全部(map(...))優化?
例子:
for item in candidate_menu:
if not item.is_max_meals_amount_ok(daily_menus):
return False
return True
我更喜歡使用功能般的風格:
all(map(operator.methodcaller('is_max_meals_amount_ok', daily_menus), candidate_menu)
我有沒有在Python all(map(...))
或any(map(...))
任何優化?
編輯:船上的Python 2.7。
標準庫中的'any'和'all'都是懶惰的 – jonrsharpe
'map()'在Python 3上是懶惰的。 – jfs