<commit_before>Quick Start
===========

To implement your decorator you need to first define a wrapper function.
This will be called each time a decorated function is called. The wrapper
function needs to take four positional arguments:

* ``wrapped`` - The wrapped function which in turns needs to be called by your wrapper function.
* ``instance`` - The object to which the wrapped function was bound when it was called.
* ``args`` - The list of positional arguments supplied when the decorated function was called.
* ``kwargs`` - The dictionary of keyword arguments supplied when the decorated function was called.

The wrapper function would do whatever it needs to, but would usually in
turn call the wrapped function that is passed in via the ``wrapped``
argument.

The decorator ``@wrapt.decorator`` then needs to be applied to the wrapper
function to convert it into a decorator which can in turn be applied to
other functions.

::

    import wrapt
    
    @wrapt.decorator
    def pass_through(wrapped, instance, args, kwargs):
        return wrapped(*args, **kwargs)

    @pass_through
    def function():
        pass

If you wish to implement a decorator which accepts arguments, then list the
arguments after the existing four arguments of the wrapper function.

::

    import wrapt
    
    @wrapt.decorator
    def with_arguments(wrapped, instance, args, kwargs, myarg1, myarg2):
        return wrapped(*args, **kwargs)

    @with_arguments(1, 2)
    def function():
        pass

It is possible to use positional arguments, with or without default values,
a variable arguments list or a keyword argument dictionary.

Any arguments given to your decorator when it is used to decorate a
function, will be passed to the wrapper via the arguments added to the
wrapper function, when the wrapper is invoked upon the call of the
decorated function.

Note that even if all your decorator arguments have default values, or you
only have a variable arguments list or keyword argument dictionary, you
must still provide the parentheses to the decorator when used. Once you opt
to have the decorator be able to accept arguments the use of the
parentheses is not optional.

If using Python 3, you can use the keyword arguments only syntax to force
use of keyword arguments when the decorator is used.

::

    import wrapt
    
    @wrapt.decorator
    def with_keyword_only_arguments(wrapped, instance, args, kwargs, *, myarg1, myarg2):
        return wrapped(*args, **kwargs)<commit_msg>Change documentation to use function closure for decorator with arguments. #1<commit_after>Quick Start
===========

To implement your decorator you need to first define a wrapper function.
This will be called each time a decorated function is called. The wrapper
function needs to take four positional arguments:

* ``wrapped`` - The wrapped function which in turns needs to be called by your wrapper function.
* ``instance`` - The object to which the wrapped function was bound when it was called.
* ``args`` - The list of positional arguments supplied when the decorated function was called.
* ``kwargs`` - The dictionary of keyword arguments supplied when the decorated function was called.

The wrapper function would do whatever it needs to, but would usually in
turn call the wrapped function that is passed in via the ``wrapped``
argument.

The decorator ``@wrapt.decorator`` then needs to be applied to the wrapper
function to convert it into a decorator which can in turn be applied to
other functions.

::

    import wrapt
    
    @wrapt.decorator
    def pass_through(wrapped, instance, args, kwargs):
        return wrapped(*args, **kwargs)

    @pass_through
    def function():
        pass

If you wish to implement a decorator which accepts arguments, then wrap the
definition of the decorator in a function closure. Any arguments supplied
to the outer function when the decorator is applied, will be available to
the inner wrapper when the wrapped function is called.

::

    import wrapt

    def with_arguments(myarg1, myarg2):
        @wrapt.decorator
        def wrapper(wrapped, instance, args, kwargs):
            return wrapper(*args, **kwargs)
        return wrapper

    @with_arguments(1, 2)
    def function():
        pass

If using Python 3, you can use the keyword arguments only syntax to force
use of keyword arguments when the decorator is used.

::

    import wrapt

    def with_keyword_only_arguments(*, myarg1, myarg2):
        @wrapt.decorator
        def wrapper(wrapped, instance, args, kwargs):
            return wrapped(*args, **kwargs)
        return wrapper
{'fraction_non_alphanumeric': 0.059079061685490875, 'fraction_numerical': 0.003258036490008688, 'mean_word_length': 3.7279260780287475, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4366811', 'n_tokens_mistral': 1135, 'n_tokens_neox': 1121, 'n_words': 622}