Hacker News new | ask | show | jobs
by kuschku 3081 days ago
> 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?

1 comments

Yes. Lambdas in Python are awkward, but you can definitely use either a lambda or a named function.

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 test
> Lambdas in Python are awkward

Can you elaborate on that, please?

Considering the examples given by the author, I think they would be a better option.

Not the OP but I think he was referring to the restrictions python puts on lambda expressions:

- they can only contain a single expression

- they cannot contain statements

Python has lambdas that have multiple expressions and statements.

They're called functions.

Python functions aren't anonymous
For precondition checking, shouldn’t that suffice?
Depends on the precondition.