liveBook · Manning

Positional argument

As explained in The Quick Python Book, Fourth Edition

A positional parameter is a function argument identified by its ordinal position in the argument list.

A positional argument is matched to a function parameter by its order in the call. The first argument fills the first parameter, the second fills the second, and so on. section 9.2.1

Use positional arguments with these rules:

  • Supply arguments in the same order as the function definition; omitting a required argument raises TypeError. section 9.2.1
  • Place parameters with default values after required parameters, because positional pairing continues through the parameter list. section 9.2.1
  • Put positional arguments before named arguments when combining passing styles; * captures extra positional arguments in a tuple. section 3.4.6 section 9.2.4

Function calls

def power(x, y=2): return x ** y power(3, 3) # 27 power(3) # 9 Here, 3 first fills x and the second 3 fills y. When y is omitted, its default value is used. section 9.2.1

Formatting strings

"{0} is the {1} of {2}".format("Ambrosia", "food", "the gods") # 'Ambrosia is the food of the gods' Numbered replacement fields refer to positional parameters regardless of where the fields appear in the format string. section 6.6.1

Listing 11.4 File opts.py
from argparse import ArgumentParser
 
def main():
    parser = ArgumentParser()
    parser.add_argument("indent", type=int, help="indent for report")
    parser.add_argument("input_file", 
                help="read data from this file")                  #1
    parser.add_argument("-f", "--file", 
                  dest="filename",                                #2
                  help="write report to FILE", metavar="FILE")
    parser.add_argument("-x", "--xray", 
                  help="specify xray strength factor")
    parser.add_argument("-q", "--quiet",
                  action="store_false", dest="verbose", 
                  default=True,                                   #3
                  help="don't print status messages to stdout")
 
    args = parser.parse_args()
 
    print("arguments:", args)
if __name__ == "__main__":
    main()

Inspect a command-line parser that defines required positional arguments alongside optional, prefixed arguments. section 11.1.5

Command-line positional arguments have no prefix such as -, are read after optional arguments, and are required unless configured otherwise. argparse stores parsed values in a namespace. section 11.1.5

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Explains order matching, defaults, mixed syntax, variadic capture, formatting indexes, and argparse requirements without implementation internals.
Examples
Demonstrates power calculations, numbered format fields, mixed function calls, and an argparse script.
What it assumes
Assumes general Python familiarity while establishing positional matching, defaults, mixed argument order, and command-line distinctions.
What you can do after
Apply positional arguments to define and call functions, format strings, and configure required argparse inputs.
How it connects
Connects positional arguments with keyword arguments, default arguments, variadic arguments, string formatting, and command-line parsing.

Also explained in

Compare how 3 books explain positional argument →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Quick Python Book, Fourth Edition. The section links point to the parts of the book it draws on.