Python: *args vs. **kwargs
Arguments can be defined as positional:
def func1(*args):
return args
>>> func1(1, 2, 3)
(1, 2, 3)
Or as * keyword*:
def func2(**kwargs):
return kwargs
>>> func2(one=1, two=2, three=3)
{'three': 3, 'two': 2, 'one': 1}
You can also do both at once:
def func3(*args, **kwargs):
print(args)
print(kwargs)
>>> func3(1, 2, a=3, b=4)
(1, 2)
{"a": 3, "b": 4}
Written by bt3gl
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Python
Authors
Related Tags
#python
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#