7 ms·
I disagree. From the end section of "real-world usage examples", there is this library https://github.com/mrocklin/multipledispatch/ https://github.com/mrocklin
by rhaps0dy 9y ago
I disagree. From the end section of "real-world usage examples", there is this library https://github.com/mrocklin/multipledispatch/ 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/expectations_quadrature.py#L63 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.
- kuschku 9y 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?
- fireflies_ 9y agoYes. 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
- ivanjr0 9y ago> 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.
- abhirag 9y agoNot 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
- jmiserez 9y agoFor precondition checking, shouldn’t that suffice?
- baq 9y agoDepends on the precondition.
- Sohcahtoa82 9y agoPython has lambdas that have multiple expressions and statements. They're called functions.
- yen223 9y agoPython functions aren't anonymous
- levi_n 9y agoI've used that multipledispatch, along my own homegrown version, in production systems.
- abhirag 9y ago"Decorators for preconditions" indeed looks neat, reminds me of Clojure which has both preconditions and postconditions built in.
- ramnes 9y agoSee my comment above. And yes, the preconditions could be written with a lambda, which would be much cleaner than using eval.