Python decorator optional argument
The trick here is, you have to introspect what you are given:
def logged(*setting_args, **setting_kwargs):
no_args = False
if len(setting_args) == 1 \
and not setting_kwargs \
and callable(setting_args[0]):
# We were called without args
func = setting_args[0]
no_args = True
def outer(func):
@wraps(func)
def with_logging(*args, **kwargs):
print "{} was called".format(func.__name__)
print "Setting args are: {}".format(setting_args)
print "Setting keyword args are: {}".format(setting_kwargs)
return func(*args, **kwargs)
return with_logging
if no_args:
return outer(func)
else:
return outer
This will work with any of the following:
# No arguments
@logged
def some_function(x):
pass
# One or more arguments
@logged(1, 2, 3)
def some_function(x):
pass
# One or more keyword arguments
@logged(key=1, another_key=2)
def some_function(x):
pass
# A mix of the two
@logged(1, 2, key=3)
def some_function(x):
pass
It will not work if it is called with only one callable argument:
# This will break.
@logged(lambda: "Just for fun")
def some_function(x):
pass
There is no way to tell the difference between a single callable setting and a no-arg invocation of the decorator. However, you can pass a garbage keyword arg to get around even that if you need to:
# This gets around the above limitation
@logged(lambda: "Just for fun", ignored=True)
def some_function(x):
pass