Skip to content

Instantly share code, notes, and snippets.

@ChengLuFred
Last active June 21, 2020 03:22
Show Gist options
  • Save ChengLuFred/c05caf0a5d4d17eeb36d8cb21f8c5461 to your computer and use it in GitHub Desktop.
Save ChengLuFred/c05caf0a5d4d17eeb36d8cb21f8c5461 to your computer and use it in GitHub Desktop.
[Pass variable number of parameters] #Python

*args pass a list of values or variables

**kwarg(key words arguments) pass a dictionary

def test_args_kwargs(arg1, arg2, arg3):
    print("arg1:", arg1)
    print("arg2:", arg2)
    print("arg3:", arg3)

# test *args
>>> args = ("two", 3, 5)
>>> test_args_kwargs(*args)
arg1: two
arg2: 3
arg3: 5

# test **kwargs:
>>> kwargs = {"arg3": 3, "arg2": "two", "arg1": 5}
>>> test_args_kwargs(**kwargs)
arg1: 5
arg2: two
arg3: 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment