Python design pattern for many conditions

前端 未结 3 1186
失恋的感觉
失恋的感觉 2021-02-05 14:32

What is the recommended structure to write validate functions with many conditions? See these two examples. The first looks ugly, the second isn\'t very common, perhaps because

3条回答
  •  一整个雨季
    2021-02-05 15:13

    The first way is much better. It can be prettified a little bit using any():

    def validate_conditions(value):
        return not any((condition(value) for condition in conditions))
    

提交回复
热议问题