Python 101 · Unpacking, Structural Matching, Packing

Feb 6, 2016 | Tech Software

Index
Index

Unpacking

  • var

    : positional, strictly based on position, 1-to-1 matching.
  • _

    : placeholder, holds a places and ignores the value to satisfy syntax requirements, 1-to-1 matching.
  • *foo

    : catch-all, catches the rest of the values and assigns them to one and only one variable, many-to-1 matching; the variable can be a placeholder variable *_ to be ignored.
  • **bar

    : mapping, unpacking key value pairs, many-to-pattern conversion; does not assign them to a variable.

Structural Matching

  • match-case

    : a fancy way of unpacking, i.e., structural unpacking a data structure to given patterns.

Packing

  • implicit tuple packing

    : assigning multiple values to a single variable, or assigning a return of a function with many values to a single variable.
  • starred list packing

    : it's actually packing many values into a single variable of list during unpacking.
  • position *args tuple

    : packing variadic positional variables into a single tuple variable.
  • keyword **kwargs dictionary

    : packing variadic keyword variables into a single dict variable.