Created
October 28, 2020 14:05
-
-
Save ChadBailey/bca6a5d68619e5b751772dc7e0d8a96f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
It may not seem like it, but this is the fruit of a lot of labor. | |
If you find it helpful, please consider crediting me in your application. | |
""" | |
import functools | |
__author__ = "Chad Bailey" | |
def decorator_demo(callable_candidate1=None, *dargs, **dkwargs): | |
def outer_core(callable_candidate2=None): | |
if callable(callable_candidate1): | |
decorator_callable = callable_candidate1 | |
decorator_args = dargs | |
elif callable(callable_candidate2): | |
decorator_callable = callable_candidate2 | |
decorator_args = (callable_candidate1,) + dargs | |
else: | |
raise RuntimeError("Decorator function called incorrectly") | |
@functools.wraps(decorator_callable) | |
def inner_core(*args, **kwargs): | |
print(f"Decorator positional arguments: {decorator_args}") | |
print(f"Decorator keyword arguments: {dkwargs}") | |
print(f"Function positional arguments: {args}") | |
print(f"Function keyword arguments: {kwargs}") | |
return decorator_callable(*args, **kwargs) | |
return inner_core | |
if callable(callable_candidate1): | |
# No positional arguments were passed to decorator | |
return outer_core(callable_candidate1) | |
return outer_core |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment