Re:Python decorators they don't tell you about
by abhirag on Sunday January 07, 2018 @11:38AM
(#16091399)
"Decorators for preconditions" indeed looks neat, reminds me of Clojure which has both preconditions and postconditions built in.
Re:Python decorators they don't tell you about
by kuschku on Sunday January 07, 2018 @08:51AM
(#16090842)
> 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?
Re:Python decorators they don't tell you about
by fireflies_ on Sunday January 07, 2018 @10:24AM
(#16091115)
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
Re:Python decorators they don't tell you about
by ivanjr0 on Sunday January 07, 2018 @12:24PM
(#16091615)
> 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.
Re:Python decorators they don't tell you about
by abhirag on Sunday January 07, 2018 @12:32PM
(#16091663)
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
Re:Python decorators they don't tell you about
by Sohcahtoa82 on Monday January 08, 2018 @08:17PM
(#16102620)
Python has lambdas that have multiple expressions and statements.
They're called functions.
Re:Python decorators they don't tell you about
by yen223 on Tuesday January 09, 2018 @03:40AM
(#16104536)
Python functions aren't anonymous
Re:Python decorators they don't tell you about
by jmiserez on Sunday January 07, 2018 @02:20PM
(#16092144)
For precondition checking, shouldn’t that suffice?
Re:Python decorators they don't tell you about
by baq on Sunday January 07, 2018 @05:18PM
(#16093058)
Depends on the precondition.
"Decorators for preconditions" look pretty neat as well. It's a little sad that the preconditions themselves have to be written inside strings though.