BobLoblaw
BobLoblaw

Reputation: 2001

Distinction between Default Argument Values and Keyword Arguments?

Are default argument values perceived as keyword arguments by Python? I'm not able understand the distinction

I can't understand this thread: Normal arguments vs. keyword arguments

On a side note, most tutorials and video series seemed to be geared towards Python 2.*. Should I learn Python 3 instead Python 2 or can I make the transition later easily? I'm just learning this out of curiosity.

Thanks in advance.

Upvotes: 6

Views: 2158

Answers (2)

Jeff - Mci
Jeff - Mci

Reputation: 1013

From what I see people tend to use both term interchangeably

I see that this question is quite old , but will add my two-cents anyway.

The term Default-Arguments ( a.k.a "Default-Parameters") is the term you will generally use while defining your function; within the header of the def statement:

When defining your function you can view it in your head as follows:

def function1(positional_parameter, default_parameter="default-output"):
    pass

However you will generally use the term "Keyword-Argument" when you're attempting to call a function.

when calling the function you can view it in your head as follows:

function1(positional_argument, keyword_argument="change-output") 
  • hope this makes sense *

Upvotes: 1

glglgl
glglgl

Reputation: 91049

Both concepts are (mostly) distinct.

On function definition side, you have named parameters which have names, and you have variadic extensions, one for positional arguments (giving a tuple) and one for keyboard arguments (giving a dict).

Example:

def f(a, b=5, *c, **d): return a, b, c, d

This function has two named parameters (aand b) which can be used positional or via keyword. c and d take all others given.

You can call this function with positional arguments as well as with keyword arguments.

f(1)
f(a=1)

both return

1, 5, (), {}

because positional and keyword arguments are assigned to the named parameters.

You can as well do

f(a=5, foo=12) or f(5, foo=12) # -> 5, 5, (), {'foo': 12}
f(1, 2, 3) # -> 1, 2, (3,), {}

In the last example, the positional arguments 1 and 2 are given to the named parameters a and b; the exceeding 3 is put into the tuple c.

You cannot do

f(b=90) # no value for a
f(12, a=90) # two values for a

If there are still unclearities, please let me know.

Upvotes: 5

Related Questions