What I'm trying to do is to pass a variable through a function that has a list of multiple lambda condition functions.
This function should behave by either returning 1 if all conditions are met or stopping the "iteration" and returning which conditions was not met.
Something like this:
def is_conditions_met(y):
conditions = (lambda x: 1 if isinstance(x, int) else return "Wrong type",
lambda x: 1 if x % 2 == 0 else return "Not even",
lambda x: 1 if x >= 40 else return "Less than 40")
return all(conditions, y)
>>> is_condition_met('foo')
>>> ' Wrong type '
>>> is_condition_met(14)
>>> ' Less than 40 '
>>> is_condition_met(42)
>>> True
CodePudding user response:
I think you should use IF, but use it more effective. In you variation all 3 functions will be executed no matter what. I suggest to check conditions step by step. For example, you don't need to execute 2 last functions if TYPE is not INT. So my variation is:
def is_conditions_met(y):
if not isinstance(y, int):
return "Wrong type"
# If type is INT we can check next condition and etc.
if not y % 2 == 0:
return "Not even"
if not y >= 40:
return "Less than 40"
return True