Search code examples
pythonfunction-parameter

In Python functions what does a stand alone * in the parameters mean


I am going through the excellent Python 3 Metaprogramming tutorial by David Beazley.

In it there is a decorator that looks as follows (slide 50):

from functools import wraps, partial

def debug(func=None, *, prefix=''):
    '''
    Decorator with or without optional arguments
    '''
    if func is None:
        return partial(debug, prefix=prefix)

    msg = prefix + func.__qualname__
    @wraps(func)
    def wrapper(*args, **kwargs):
        print(msg)
        return func(*args, **kwargs)
    return wrapper

In the parameters of the function there is a * that is between the keyword parameter func and prefix. I have tested the decorator with or without the star and in both cases it works.

My question is - what if any, is the purpose of the *?


Solution

  • It marks the end of the parameters that may be provided by position only. The arguments following the * must be specified as keyword arguments. See the PEP for more info.