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


