Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 :

  • What is the meaning of a required argument and an optional argument? Is the default argument also a keyword argument? (since because both contain "=")

  • Difference between the positional, keyword, optional, and required arguments?

  • python official documentation says that there are two types of arguments. If so, then what are *args and **kargs (I know how they work but don't know what they are)

  • how *args and **kargs store values? I know how *args and **kargs works but how do they store values? Does *args store values in a tuple and **kargs in the dictionary?

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

Thanks in advance

like image 698
micro192 Avatar asked Oct 07 '16 23:10

micro192


People also ask

Why do the positional arguments appear after the keyword argument P1=21?

because the positional arguments 43 and 11 appears after the keyword argument p1=21. Python allows us to create functions with parameters that have default values. The default values are passed to the parameters when the function is invoked without the arguments. Let's define a function with default arguments.

Are positional and required arguments the same in Python?

Since Python 3.8 introduced positional-only parameters, this post needed an update. Positional arguments, keyword arguments, required arguments and optional arguments are often confused. Positional arguments ARE NOT THE SAME AS required arguments, and keywords arguments ARE NOT THE SAME AS optional arguments.

What is an optional keyword argument?

A keyword argument is just a positional argument with a default value. You must specify all arguments that don't have a default value. In other words, keyword arguments are only "optional" because they will be set to their default value if not specifically supplied.

What is the difference between keyword arguments and required arguments?

Keyword arguments are arguments that can be called by their name. Required arguments are arguments that must passed to the function. Optional arguments are argument that can be not passed to the function. In python optional arguments are arguments that have a default value.


1 Answers

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()
like image 166
IzPrEE Avatar answered Sep 30 '22 15:09

IzPrEE