Sunday, 23 July 2017

args and kwargs in python

Normal Arguments and arbitrary arguments and keyword arguments in python:


  • Normal argument type depends on value/data
  • Arbitrary argument by default type is tuple, Represents *
  • keyword argument  by default type is dictionary,Represents **


Example:

def f1(req,*args,**kwargs):
    if req:
        print(req,type(req))
    if args:
        print(args,type(args))
    if kwargs:
        print(kwargs,type(kwargs))

f1(10)

f1(10,2.3,5,"siva")

f1(10,2.3,5,"siva",key1=20,key2="siva",key3=True)

Output:

10  <class 'int'>

10  <class 'int'>
(2.3, 5, 'siva')  <class 'tuple'>

10  <class 'int'>
(2.3, 5, 'siva')  <class 'tuple'>

{'key1': 20, 'key2': 'siva', 'key3': True}  <class 'dict'>

No comments:

Post a Comment