Reputation: 35982
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?
Upvotes: 135
Views: 239739
Reputation: 8027
Here is some extra information to complete @Nazime Lakehal’s excellent answer.
A positional parameter that is optional cannot be followed by a positional parameter or positional-or-keyword parameter that is required:
# SyntaxError.
def f(a=1, b, /):
pass
# SyntaxError.
def f(a=1, /, b):
pass
A positional-or-keyword parameter that is optional cannot be followed by a positional-or-keyword parameter that is required:
# SyntaxError.
def f(a=1, b):
pass
For binding a positional parameter that is optional, all previous positional parameters that are optional have to be bound, making them all effectively required. That may be the origin of the confusion of positional parameters with required parameters:
def f(a=1, b=2, /):
pass
f(1, 0)
For binding a keyword parameter or positional-or-keyword parameter that is optional, all other keyword parameters and positional-or-keyword parameters that are optional do not have to be bound. That may be the origin of the confusion of keyword parameters with optional parameters:
def f(c=3, *, a=1, b=2):
pass
f(b=0)
def f(a=1, b=2, *, c=3):
pass
f(b=0)
Upvotes: 2
Reputation: 1782
Positional parameters, keyword parameters, required parameters and optional parameters are often confused. Positional parameters are not the same as required parameters, and keywords parameters are not the same as optional parameters.
Positional(-only) parameters are bound to positional arguments provided in a call, that is by position. They were introduced in Python 3.8.
Keyword(-only) parameters are bound to keyword arguments provided in a call, that is by name.
Positional-or-keyword parameters are bound to positional arguments or keyword arguments provided in a call, that is either by position or by name.
Required parameters are bound to arguments provided in a call.
Optional parameters are bound to default arguments provided in a definition.
This is the Python syntax for declaring parameters:
def f(positional_parameter, /, positional_or_keyword_parameter, *, keyword_parameter):
pass
Positional parameter that is required (since Python 3.8):
def f(a, /):
pass
f() # error, argument is required
f(1) # allowed, positional argument
f(a=1) # error, keyword argument
Positional parameter that is optional (since Python 3.8):
def f(a=2, /):
pass
f() # allowed, argument is optional
f(1) # allowed, positional argument
f(a=1) # error, keyword argument
Keyword parameter that is required:
def f(*, a):
pass
f() # error, argument is required
f(1) # error, positional argument
f(a=1) # allowed, keyword argument
Keyword parameter that is optional:
def f(*, a=1):
pass
f() # allowed, argument is optional
f(1) # error, positional argument
f(a=1) # allowed, keyword argument
Positional-or-keyword parameter that is required:
def f(a):
pass
f() # error, argument is required
f(1) # allowed, positional argument
f(a=1) # allowed, keyword argument
# In fact that function is the same as this one.
def f(/, a, *):
pass
Positional-or-keyword parameter that is optional:
def f(a=1):
pass
f() # allowed, argument is optional
f(1) # allowed, positional argument
f(a=1) # allowed, keyword argument
# In fact that function is the same as this one.
def f(/, a=1, *):
pass
Conclusion. — A parameter can be required or optional but not both at the same time. It can also be positional, keyword, or both at the same time.
Upvotes: 126
Reputation: 7242
First, a parameter is a named entity in the function/method definition that specifies an argument. An argument is a value passed to a function.
For example,
def rectangle_area(height, width):
pass
rectangle_area(argument_1, argument_2)
height, width
are the function parameters, and argument_1, argument_2
are the arguments passed to the function. When you say positional argument, you are talking about arguments, this has nothing to do with the function definition. width
and height
are (by default in Python) positional parameters or keyword parameters (so called positional-or-keyword parameters). Therefore, you could pass arguments either positionally or by keywords.
How you are calling/passing the value to the function determines if they are positional arguments or keyword arguments.
For the function rectangle_area
we could call it equally like so:
rectangle_area(1, 2) # positional arguments
rectangle_area(width=2, height=1) # keyword arguments
1, 2
we mean height is 1 and width is 2 based on the position they are passed (i.e., in the function definition the first parameter is the height
and the second is the width
).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.
Upvotes: 13
Reputation: 881293
That text you quote seems to be confused about two totally different things:
5.3.4 Calls
).7.6 Function definitions
I suspect the people who put together that course-ware weren't totally familiar with Python :-) Hence that link you provide is not a very good quality one.
In your call to your function, you're using the "keyword argument" feature (where the argument is named rather than relying on its position). Without that, values are bound to names based on order alone. So, in this example, the two calls below are equivalent:
def process_a_and_b(a, b):
blah_blah_blah()
process_a_and_b(1, 2)
process_a_and_b(b=2, a=1)
By further 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.
Upvotes: 208
Reputation: 7130
I assume that both width and height are positional arguments. Then why can we also call it with the keyword argument syntax?
To prevent that you can use positional-only arguments:
def rectangleArea(width, height, /):
return width * height
print rectangleArea(width=1, height=2)
The error message would be as follows:
TypeError: rectangleArea() got some positional-only arguments passed as keyword arguments: 'width, height'
Upvotes: 0
Reputation: 21
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.
Upvotes: 0
Reputation: 21
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.
Upvotes: 1
Reputation: 78590
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))
Upvotes: 1
Reputation: 971
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.
Upvotes: 20