Skip to main content
updated code
Source Link
Vinod Srivastav
  • 4.3k
  • 1
  • 34
  • 45

Both *args & **kwargs allows us to pass multiple arguments/parameters to a python method but there are few differences explained below:

*args

Is used to pass n-numbers of arguments in order much like a Array every element will follow and index.

def foo(x, y, *args):
    pass

def bar(x, y, **kwargs):
    pass

*args

As far as I know, *args is an array of arguments separated by comma , so if you wanted to to foo above it will look like

foo("x","y",1,2,3,4,5)

so if you run

for a in args:
        print(a)  

it will print arguments in order of placement as 1,2,3...

Although this is very easy to implement and use the order of arguments matters a lot here. So if the first argument is supposed to be a string and second and integer, if the caller messed with the order the function fails.


**kwargs

These are called keyword arguments here also you can pass variable numbers of arguments but the arguments are much like a dictionary k/v pair.

def bar(x, y, **kwargs):
    pass

These are keyword arguments which are set of named arguments which are passed as key/value pair or dictionary separated by , if multiple. So for bar you can send

bar("x", "y", name="vinod",address="bangalore",country="india")

and can read it in the function individually as

Name = kwargs['name']
Address = kwargs['address'] 

Reading kwargs doesn't required to enumerate over a loop and order of arguments don't matter.

def foo(x, y, *args):
    pass

def bar(x, y, **kwargs):
    pass

*args

As far as I know, *args is an array of arguments separated by comma , so if you wanted to to foo above it will look like

foo("x","y",1,2,3,4,5)

so if you run

for a in args:
        print(a)  

it will print arguments in order of placement as 1,2,3...

Although this is very easy to implement and use the order of arguments matters a lot here. So if the first argument is supposed to be a string and second and integer, if the caller messed with the order the function fails.


**kwargs

These are keyword arguments which are set of named arguments which are passed as key/value pair or dictionary separated by , if multiple. So for bar you can send

bar("x", "y", name="vinod",address="bangalore",country="india")

and can read it in the function individually as

Name = kwargs['name']
Address = kwargs['address'] 

Reading kwargs doesn't required to enumerate over a loop and order of arguments don't matter.

Both *args & **kwargs allows us to pass multiple arguments/parameters to a python method but there are few differences explained below:

*args

Is used to pass n-numbers of arguments in order much like a Array every element will follow and index.

def foo(x, y, *args):
    pass

As far as I know, *args is an array of arguments separated by comma , so if you wanted to to foo above it will look like

foo("x","y",1,2,3,4,5)

so if you run

for a in args:
        print(a)  

it will print arguments in order of placement as 1,2,3...

Although this is very easy to implement and use the order of arguments matters a lot here. So if the first argument is supposed to be a string and second and integer, if the caller messed with the order the function fails.


**kwargs

These are called keyword arguments here also you can pass variable numbers of arguments but the arguments are much like a dictionary k/v pair.

def bar(x, y, **kwargs):
    pass

These are keyword arguments which are set of named arguments which are passed as key/value pair or dictionary separated by , if multiple. So for bar you can send

bar("x", "y", name="vinod",address="bangalore",country="india")

and can read it in the function individually as

Name = kwargs['name']
Address = kwargs['address'] 

Reading kwargs doesn't required to enumerate over a loop and order of arguments don't matter.

fixed spell mistake
Source Link
Vinod Srivastav
  • 4.3k
  • 1
  • 34
  • 45
def foo(x, y, *args):
    pass

def bar(x, y, **kwargs):
    pass

*args

As far as I know, *args is an array of arguments separated by comma , so if you wanted to to foo above it will look like

foo("x","y",1,2,3,4,5)

so if you run

for a in args:
        print(a)  

it will print arguments in order of placement as 1,2,3...

Although this is very easy to implement and use the order of arguments matters a lostlot here. So if the first argument is supposed to be a string and second and integer, if the caller messed with the order the function fails.


**kwargs

These are keyword arguments which are set of named arguments which are passed as key/value pairkey/value pair or dictionary separated by , if multiple. So for bar you can send

bar("x", "y", name="vinod",address="bangalore",country="india")

and can read it in the function individually as

Name = kwargs['name']
Address = kwargs['address'] 

Reading kwargs doesn't required to enumerate over a loop and order of arguments don't matter.

def foo(x, y, *args):
    pass

def bar(x, y, **kwargs):
    pass

*args

As far as I know, *args is an array of arguments separated by comma , so if you wanted to to foo above it will look like

foo("x","y",1,2,3,4,5)

so if you run

for a in args:
        print(a)  

it will print arguments in order of placement as 1,2,3...

Although this is very easy to implement and use the order of arguments matters a lost here. So if the first argument is supposed to be a string and second and integer, if the caller messed with the order the function fails.


**kwargs

These are keyword arguments which are set of named arguments which are passed as key/value pair separated by , if multiple. So for bar you can send

bar("x", "y", name="vinod",address="bangalore",country="india")

and can read it in the function individually as

Name = kwargs['name']
Address = kwargs['address'] 

Reading kwargs doesn't required to enumerate over a loop and order of arguments don't matter.

def foo(x, y, *args):
    pass

def bar(x, y, **kwargs):
    pass

*args

As far as I know, *args is an array of arguments separated by comma , so if you wanted to to foo above it will look like

foo("x","y",1,2,3,4,5)

so if you run

for a in args:
        print(a)  

it will print arguments in order of placement as 1,2,3...

Although this is very easy to implement and use the order of arguments matters a lot here. So if the first argument is supposed to be a string and second and integer, if the caller messed with the order the function fails.


**kwargs

These are keyword arguments which are set of named arguments which are passed as key/value pair or dictionary separated by , if multiple. So for bar you can send

bar("x", "y", name="vinod",address="bangalore",country="india")

and can read it in the function individually as

Name = kwargs['name']
Address = kwargs['address'] 

Reading kwargs doesn't required to enumerate over a loop and order of arguments don't matter.

Source Link
Vinod Srivastav
  • 4.3k
  • 1
  • 34
  • 45

def foo(x, y, *args):
    pass

def bar(x, y, **kwargs):
    pass

*args

As far as I know, *args is an array of arguments separated by comma , so if you wanted to to foo above it will look like

foo("x","y",1,2,3,4,5)

so if you run

for a in args:
        print(a)  

it will print arguments in order of placement as 1,2,3...

Although this is very easy to implement and use the order of arguments matters a lost here. So if the first argument is supposed to be a string and second and integer, if the caller messed with the order the function fails.


**kwargs

These are keyword arguments which are set of named arguments which are passed as key/value pair separated by , if multiple. So for bar you can send

bar("x", "y", name="vinod",address="bangalore",country="india")

and can read it in the function individually as

Name = kwargs['name']
Address = kwargs['address'] 

Reading kwargs doesn't required to enumerate over a loop and order of arguments don't matter.