args and Kwargs in python: basics

Args and kwargs are used in python to pass variable arguments to a function.

All Non-keyworded arguments in a function are handled using args and all the keyworded arguments are handled using kwargs.

args are nothing but tuples and kwargs are dictionaries. All methods applicable on tuples are applicable on args and methods on dictionaries are applicable on kwargs.

def testfunc(*args, **kwargs):
    print("args which are passed: {0}. The type is {1}".format(args,type(args)))
    print("args which are passed: {0}. The type is {1}".format(kwargs,type(kwargs)))


Output of the above program is below:

args which are passed: ('hi', {'a': 1}). The type is <class 'tuple'>
args which are passed: {'b': 2}. The type is <class 'dict'>

No Comments Yet