fredgj / mom

The mother of all decorators

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Mom

Mom is a python module that contains one single decorator called decorate_with, but this is also a powerful decorator. It's the mother of all decorators.

In the future I might add some more meta stuff here, but at the moment I only need this single decorator.

It might not be the most useful decorator, but it intends to solve a problem, at least for some people.

Usage

decorate_with is a decorator that takes other decorators as input, including their arguments and/or keyword arguments (if any), in order to apply them to a function.

Some people think decorating their functions with several decorators like in the example below looks way too ugly.

decorate_with lets you decorate your functions with all the decorators you need in one single "decoration".

As you can see in the example above, decorate_with takes other decorators as arguments and applies them to the function. If the decorator has no arguments you can just pass it in directly, or as a list, it's up to the user. Though, passing them in directly looks slightly more elegant. If the decorator has any arguments you need to add it to a list together its arguments.

If a decorator has been defined with default arguments you need to pass the keyword arguments dictionary to decorate_with, in case you have any keyword arguemnts to pass.

First, lets write a simple decorator with two default arguments. In the reality there are three default arguments, but func will be overwritten by wrapper's scope inside the else part if msg1 and/or msg2 have a value.

Now we can apply it like this:

Or without any arguments

About

The mother of all decorators

License:MIT License


Languages

Language:Python 100.0%