Created
November 15, 2018 17:44
-
-
Save iamaziz/f6a504e562bed9fc5c070ac5b0d7876e to your computer and use it in GitHub Desktop.
How to use *args and **kwargs with a wrapper function
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
# how to use *args and **kwargs with wrapper | |
def main_function(a: int, b: int): | |
print(f'a: {a} + b: {b} = {a+b}') | |
def my_wrapper(*args, **kwargs): | |
print(f'wrapper recieved: {len(args)} args and {len(kwargs)} kwargs') | |
if 'a' not in kwargs: kwargs.update({'a': 6}) | |
if 'b' not in kwargs: kwargs.update({'b': 8}) | |
return main_function(**kwargs) | |
""" Example | |
>>> my_wrapper() | |
wrapper recieved: 0 args and 0 kwargs | |
a: 6 + b: 8 = 14 | |
>>> my_wrapper(a=5, b=10) | |
wrapper recieved: 0 args and 2 kwargs | |
a: 5 + b: 10 = 15 | |
>>> my_wrapper('hello') | |
wrapper recieved: 1 args and 0 kwargs | |
a: 6 + b: 8 = 14 | |
>>> my_wrapper('hello', a=3) | |
wrapper recieved: 1 args and 1 kwargs | |
a: 3 + b: 8 = 11 | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment