> It's a little sad that the preconditions themselves have to be written inside strings though.
Isn’t it possible to pass a lambda instead?
Isn’t it possible to pass a lambda instead?
In fact, you could define a number of useful precondition functions in a single module and use them throughout a project. A couple of higher-order functions could make the post's examples safer and nicer-looking too. For example:
@precondition(starts_with('The year is '))
where the starts_with precondition is: def starts_with(param):
def test(s):
return s.startswith(param)
return testCan you elaborate on that, please?
Considering the examples given by the author, I think they would be a better option.
- they can only contain a single expression
- they cannot contain statements
They're called functions.