Question :
Based on this
A positional argument is a name that is not followed by an equal sign
(=) and default value.A keyword argument is followed by an equal sign and an expression that
gives its default value.
def rectangleArea(width, height):
return width * height
print rectangleArea(width=1, height=2)
Question> I assume that both width
and height
are positional arguments. Then why can we also call it with the keyword argument syntax?
Answer #1:
That text you quote is for the definition of the function and has nothing to do with calls to the function. In the call to that function, you’re using the “named argument” feature. That link you provide is not a very good quality one, the authors seem confused between two different things.
The Python reference refers to positional and keyword arguments only in respect to a call to a function (see section 5.3.4 Calls
).
When they talk about the definition of a function in section 7.6 Function definitions
, it’s a totally different term “default parameter values”.
I suspect the people who put together that course-ware weren’t totally familiar with Python 🙂
By way of example, refer to the following definition and calls:
def fn (a, b, c = 1): # a/b required, c optional.
return a * b + c
print fn (1, 2) # returns 3, positional and default.
print fn (1, 2, 3) # returns 5, positional.
print fn (c = 5, b = 2, a = 2) # returns 9, named.
print fn (b = 2, a = 2) # returns 5, named and default.
print fn (5, c = 2, b = 1) # returns 7, positional and named.
print fn (8, b = 0) # returns 1, positional, named and default.
The meaning of the =
changes, depending on whether it’s in the definition or in the call.
In the definition, it marks the argument optional and sets a default value.
In the call, it simply allows you to specify which arguments should be which values, in whatever order you want.
Answer #2:
Since python 3.8 introduced positional arguments only, this post need 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.
Positional arguments are arguments that can be called by their position in the function definition.
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.
Positional argument that is optional (python 3.8)
def f(a=2, /):
pass
f() # Allowed, argument is optional
f(1) # Allowed, it's a positional argument
f(a=1) # Error, positional only argument
Positional argument that is required (python 3.8)
def f(a, /):
pass
f() # Error, argument required
f(1) # Allowed, it's a positional argument
f(a=1) # Error, positional only argument
Keyword argument that is optional
def f(*, a=1):
pass
f() # Allowed
f(1) # Error, keyword only arguments
f(a=1) # Allowed, it's a keyword argument
keyword argument that is required
def f(*, a)
pass
f() # Error, argument required
f(1) # Error, keyword only arguments
f(a=1) # Allowed, it's a keyword argument
Positional and keyword argument that is optional
def f(a=1)
pass
f() # Allowed, argument is optional
f(1) # Allowed, it's a positional argument
f(a=1) # Allowed, it's a keyword argument
# In fact this function is the same as
def f(*, a=1, /):
pass
Positional and keyword argument that is required
def f(a):
pass
f() # Error, argument required
f(1) # Allowed, it's a positional argument
f(a=1) # Allowed, it's a keyword argument
# In fact this function is the same as
def f(*, a, /):
pass
Conclusion, an argument can be optional or required not both at the same time. It can also be positional, keyword or both at the same time.
Python 3.8 introduced positional only parameters.
def f(positional_argument, /, positional_or_keyword_argument, *, keyword_argument):
pass
Answer #3:
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.
Answer #4:
Positional arguments can be called either using values in order or by naming each. For example, all three of the following would work the same way:
def rectangleArea(width, height):
return width * height
print(rectangleArea(1, 2))
print(rectangleArea(width=1, height=2))
print(rectangleArea(height=2, width=1))
Answer #5:
positional arguments: arguments passed to a function in correct positional order. below program understand the positional arguments of a function
#positional arguments example
def combine(str1, str2):
#To join str1 and str2 with str3
str3 = str1 + str2
print(str3)
#call combine() and pass 2 strings
combine("Well", "come") #positional arguments
suppose, we passed ‘come’ first, ‘well’ second, then the result will be comewell. also, call the function 3 strings become error.
Answer #6:
Understand the keyword arguments of a function.
Keyword arguments are arguments that identify the parameters by their names.
#keyword arguments example:
def employee(name, Id):
print("Employee Name: ", name)
print("Employee Id : ", Id)
#call employee() and pass 2 arguments
employee(name = "inban", Id = "pay001")
employee(Id = "pay002", name = "karthik") #we can change the order args.
Answer #7:
Defining parameters and arguments here could help.
- Parameter: a named entity in the function/method definition that specifies an argument.
- Argument: a value passed to a function.
For example,
def my_function(parameter_1, parameter_2):
pass
my_function(argument_1, argument_2)
Now when you say positional argument, you are talking about arguments, so has nothing to do with the function definition. width
and height
in your example are positional parameters or keyword parameters (so called positional-or-keyword parameters).
How you are calling/passing the value to the function determines if they are positional arguments or keyword arguments.
rectangleArea(1, 2) # positional arguments
rectangleArea(width=1, height=2) # keyword arguments
The thing not many people know is that you can specify a positional-only parameter by using the /
in the parameter list (example from here).
def func(positional_only1, positional_only2, /, positional_or_keyword): ...
Similarly, you can also have keyword-only parameters by using the *
character.
def func(positional_or_keyword, *, keyword_only1, keyword_only2): ...
Finally, we also have var-positional and var-keyword (a.k.a *args and **kwargs respectively). Meaning, you can have arbitrary sequence of positional arguments or keyword arguments passed to the function.