7

What does the * mean in the following code (found in the pprint library)?

def pformat(object, indent=1, width=80, depth=None, *, compact=False):
    """Format a Python object into a pretty-printed representation."""
    return PrettyPrinter(indent=indent, width=width, depth=depth,
                         compact=compact).pformat(object)

If it was *args then it would be an arbitrary number of positional parameters. The parameter values would be in the tuple called args. The first 4 parameters could be assigned either by name or by position, the parameter compact could only be assigned by name...

Well, NO! Because it doesn't agree with the documentation:

In a function call, keyword arguments must follow positional arguments.

So, what does the star do after and before other named arguments? And how is that used? Or why is it there if it is not used?

stenci
  • 8,290
  • 14
  • 64
  • 104

1 Answers1

11

It separates positional arguments from keyword-only arguments when there are no variable arguments. This is a Python-3-only feature.

BrenBarn
  • 242,874
  • 37
  • 412
  • 384
  • Thanks. In the question I referred to the Python 2 documentation page, but the quoted phrase is also in the 3. – stenci Sep 22 '15 at 22:47