Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Positional argument v.s. keyword argument

Tags:

python

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?

like image 901
q0987 Avatar asked Feb 26 '12 04:02

q0987


People also ask

What are positional arguments?

Positional arguments are arguments that need to be included in the proper position or order. The first positional argument always needs to be listed first when the function is called. The second positional argument needs to be listed second and the third positional argument listed third, etc.

What is meant by positional argument follows keyword argument?

In Python, the SyntaxError: positional argument follows keyword argument occurs if you pass keyword arguments before the positional arguments. Since Python interprets positional arguments in the order in which they appear first and then followed by the keyword arguments as next.

Can we mix positional and keyword arguments?

Keyword arguments aren't just useful for functions that accept any number of positional arguments (like print ). You can pass keyword arguments to just about any function in Python. We're passing in one positional argument and one keyword argument. That start=1 works with sum because start is the name of that argument.

What is a keyword argument?

Keyword arguments (or named arguments) are values that, when passed into a function, are identifiable by specific parameter names. A keyword argument is preceded by a parameter and the assignment operator, = . Keyword arguments can be likened to dictionaries in that they map a value to a keyword.


2 Answers

That text you quote seems to be confused about two totally different things:

  • Positional and keyword arguments are a feature of calls to a function (see Python reference section 5.3.4 Calls).
  • Default values are a feature of function definitions, as per section 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. 
like image 144
paxdiablo Avatar answered Oct 01 '22 07:10

paxdiablo


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.

Positional arguments are arguments that can be called by their position in the function call.

Keyword arguments are arguments that can be called by their name.

Required arguments are arguments that must passed to the function.

Optional arguments are arguments 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 argument 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-or-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-or-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 but 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 
like image 44
Nazime Lakehal Avatar answered Oct 01 '22 07:10

Nazime Lakehal