micro192
micro192

Reputation: 97

Difference between Positional , keyword, optional and required argument?

I am learning about functions in python and found many good tutorials and answers about functions and their types, but I am confused in some places. I have read the following:

If function has "=" then it's a keyword argument i.e (a,b=2) If function does not have "=" then it's positional argument i.e (a,b)

My doubts :

please explain in deep. I want to know about functions because I am a newbie :)

Thanks in advance

Upvotes: 7

Views: 6760

Answers (2)

IzPrEE
IzPrEE

Reputation: 313

Default Values

Let's imagine a function,

def function(a, b, c):
    print a
    print b
    print c

A positional argument is passed to the function in this way.

function("position1", "position2", "position3")

will print

position1
position2
position3

However, you could pass in a keyword argument as below,

function(c="1",a="2",b="3")

and the output will become:

2
3
1

The input is no longer based on the position of the argument, but now it is based on the keyword.

The reason that b is optional in (a,b=2) is because you are giving it a default value.

This means that if you only supply the function with one argument, it will be applied to a. A default value must be set in the function definition. This way when you omit the argument from the function call, the default will be applied to that variable. In this way it becomes 'optional' to pass in that variable.

For example:

def function(a, b=10, c=5):
    print a
    print b
    print c

function(1)

and the output will become:

1
10
5

This is because you didn't give an argument for b or c so they used the default values. In this sense, b and c are optional because the function will not fail if you do not explicitly give them.

Variable length argument lists

The difference between *args and **kwargs is that a function like this:

def function(*args)
    for argument in args:
        print argument

can be called like this:

function(1,2,3,4,5,6,7,8)

and all of these arguments will be stored in a tuple called args. Keep in mind the variable name args can be replaced by any variable name, the required piece is the asterisk.

Whereas,

def function(**args):
    keys = args.keys()
    for key in keys:
       if(key == 'somethingelse'):
           print args[key]

expects to be called like this:

function(key1=1,key2=2,key3=3,somethingelse=4,doesnt=5,matter=6)

and all of these arguments will be stored in a dict called args. Keep in mind the variable name args can be replaced by any variable name, the required piece is the double asterisk.

In this way you will need to get the keys in some way:

keys = args.keys()

Upvotes: 9

Chad S.
Chad S.

Reputation: 6633

  • Optional arguments are those that have a default or those which are passed via *args and **kwargs.
  • Any argument can be a keyword argument, it just depends on how it's called.
  • these are used for passing variable numbers of args or keyword args
  • yes and yes

For more information see the tutorial:

https://docs.python.org/2/tutorial/controlflow.html#more-on-defining-functions

Upvotes: 0

Related Questions