PEP 802: Display Syntax for the Empty Set

I agree that the use of a / here feels weird when it has no logical connection to other parts of Python, but what if it does?

Namely, if we generalize / as a sentinel object to denote an absence of value such that an item of / in a sequence, or a key with with a value of / in a mapping, is omitted, then {/} as an empty set becomes just a natural byproduct..

Possible use cases:

  1. Conditionally building command-line arguments:
# equivalent to subprocess.run(['cmd']) when DEBUG is false
subprocess.run(['cmd', '--debug' if DEBUG else /])
  1. Conditionally specifying keyword arguments inline:
# ast.dump(tree) when Python version < 3.8
ast.dump(tree, indent=4 if sys.version_info >= (3, 8) else /)
  1. A mapping function that can conditionally skip output:
def func(num):
    if num > 5:
        return /
    return num * 2

list(map(func, range(1, 10))) # [2, 4, 6, 8, 10]

/ can be remembered as part of a prohibition sign :prohibited:, though in a different orientation.

6 Likes