0

The Python code below throws an error.

def f(a,*, **c):
    pass

The error says that SyntaxError: named arguments must follow bare *. I couldn't understand what this means in this case. I have specified a parameter after the bare *, yet I get an error.

coderboy
  • 1,710
  • 1
  • 6
  • 16
  • The interpret means that you must specify `something` immediately after `*`, not add another argument after `*`. – Captain Trojan Dec 08 '20 at 17:36
  • * is not an argument. pass something with * – mhhabib Dec 08 '20 at 17:37
  • It's valid to write something like ```def f(a, *, c)```. So the fact that something must follow immediately after ```*```, is not right, I guess. – coderboy Dec 08 '20 at 17:41
  • Please repeat [on topic](https://stackoverflow.com/help/on-topic) and [how to ask](https://stackoverflow.com/help/how-to-ask) from the [intro tour](https://stackoverflow.com/tour). If nothing else, we expect you to look up the error message before posting here. – Prune Dec 08 '20 at 17:41
  • `**c` is not, strictly speaking, a named argument, as it represents an arbitrary (and possibly empty) collection of keyword arguments. – chepner Dec 08 '20 at 17:41
  • 1
    I was certain I saw this question somewhere already, finally found it: https://bugs.python.org/issue2613 – cglacet Dec 08 '20 at 17:46
  • @cglacet You're link was so helpful. Believe me. thanks for it.. – coderboy Dec 08 '20 at 17:50

1 Answers1

-1

A standalone * parameter is only necessary (and as you've seen, allowed) if you specify one or more named keyword-only parameters following it. Since you have no named keyword-only parameters, you need to omit it.

def f(a, **c):
    pass

If you want a to be a positional-only argument, you need to use a standalone / parameter:

def f(a, /, **c):
    pass
chepner
  • 497,756
  • 71
  • 530
  • 681
  • I think it's important to note that `f(2, 3)` would raise an error (as I guess the OP expects): `f() takes 1 positional argument but 2 were given` – cglacet Dec 08 '20 at 17:43