Comment by kristjansson

2 days ago

While not nearly as fun as the OP, I’d note that this sort of unpacking is very pleasant in the newish PEP636 match case statements:

https://peps.python.org/pep-0636/#matching-builtin-classes

Looks really cool!

Will this allow combinations of bound and unbound variables?

E.g.:

  def is_on_horizontal_line(point, line_y):
    match point:
      case (x, line_y):
        return f"Yes, with x={x}"
      case _:
        return "No"

Seems both useful and potentially confusing.

  • It allows you to use bound variables/constants as long as the expression includes a dot so you can distinguish it from a capture variable.

    Scala allows matching against bound variables but requires it either start with an uppercase letter or be surrounded in backtics in the pattern. I don't know that that would make sense for python, but there could potentially be some special syntax to spicify you want to compare against an existing variable instead of capturing a new variable.

    • Ah, that makes sense. Maybe the "exceptions" (dots, uppercase letters, etc) are needed to permit bound variables that we usually don't think of as variables at all, like class or package identifiers?