liveBook · Manning

Positional argument

As explained in Django in Action

A Python positional argument is an argument assigned to a parameter according to its position in the argument list.

A positional argument is assigned according to its place in an argument list. On a command line, the first value fills the first expected position, the second fills the second, and so on. This differs from a flag, which is an optional named switch that changes a command’s behavior. section 9.3

Keep these two uses separate:

  • Command-line positional arguments carry data or filters by position; Unix zip uses the archive name first, followed by files to include. section 9.3
  • Django management commands receive command-line strings through argparse, while flags such as --verbose or -v are optional behavior controls. section 9.3
  • Django’s generic Model.objects.create() method does not accept positional field values; supply field names as keyword arguments instead. section 4.4

Filtering musicians from the terminal

A musicians management command can be run with filters such as `python manage.py musicians --last_name Loblaw`. Here, the filter value belongs to the named `--last_name` option rather than to a positional slot. By contrast, a positional command could interpret its first value as one kind of filter and its second value as another, based only on their order. section 9.3

How Django in Action covers it

How deep it goes
Defines positional placement, contrasts flags, and stops short of implementing a positional Django command.
Examples
Uses Unix zip positions, Django musician filters, and ORM creation that requires named field values.
What it assumes
Assumes basic command-line use, while introducing flags, argparse parsing, and the distinction between parameters and arguments.
What you can do after
Apply positional arguments in command-line interfaces and choose keyword arguments when calling Django’s generic ORM create method.
How it connects
Connects positional arguments with command-line arguments, flags, argparse, parameters, and keyword arguments.

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 Django in Action. The section links point to the parts of the book it draws on.