A Python Function for Unpacking Args and Kwargs

Python

Example of using for loops to unpack and print args and kwargs. The python single asterisk operator is used to pass positional arguments passed while the double asterisk is used for keyword arguments.

 1|  def print_args(*args,**kwargs):
 2|      for i in args:
 3|          print(i)
 4|      for j in kwargs:
 5|          print('{0}: {1}'.format(j, kwargs[j]))
 6|  
 7|  print_args(1,2,3,a=10,b=20)
 8|  
 9|  >> 1
10|  >> 2
11|  >> 3
12|  >> a: 10
13|  >> b: 20
Did you find this snippet useful?

Sign up for free to to add this to your code library