Skip to content

Latest commit

 

History

History
54 lines (38 loc) · 948 Bytes

args-kwargs.md

File metadata and controls

54 lines (38 loc) · 948 Bytes

*args and **kwargs

A normal function:

def func1(one, two)
  print(one)
  print(two)

func1('arg one', 'arg two')  # Correct
func1('arg one')  # Error
func1('arg one', 'arg two', 'arg three')  # Error

*args usage:

def func2(*args)
  for stuff in args:
    print(stuff)
    
my_list = ['green', 'yellow', 'blue', 'red']

func2(*my_list)  # Correct
func2('green', 'yellow', 'blue', 'red')  # Correct
def func3(one, two, *args)
  print(one)
  print(two)
  for stuff in args:
    print(stuff)
    
my_list = ['green', 'yellow', 'blue', 'red']

func3('required one', 'required two', *my_list)  # Correct

**kwargs usage:

def func4(**kwargs)
  for key, value in kwargs.items():
    print(key)
    print(value)
    
my_dict = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}

func4(**kwargs)  # Correct
func4(key1 = 'value1', key2 = 'value2', key3 = 'value3')  # Correct