News for hackers. Stuff that matters.
Re:Python decorators they don't tell you about by rhaps0dy on Sunday January 07, 2018 @06:41AM (#16090466)
I disagree. From the end section of "real-world usage examples", there is this library https://github.com/mrocklin/multipledispatch/ that implements "Decorators for registration. It's used in at least one (probably many more) serious open-source project that I know of: GPflow https://github.com/GPflow/GPflow/blob/master/gpflow/expectat... .

"Decorators for preconditions" look pretty neat as well. It's a little sad that the preconditions themselves have to be written inside strings though.

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.
Re:Python decorators they don't tell you about by levi_n on Sunday January 07, 2018 @09:03AM (#16090883)
I've used that multipledispatch, along my own homegrown version, in production systems.
Re:Python decorators they don't tell you about by ramnes on Sunday January 07, 2018 @12:33PM (#16091666)
See my comment above. And yes, the preconditions could be written with a lambda, which would be much cleaner than using eval.