1

Why in python you cannot do:

def foo(bar, *, **kwargs):
    pass

but can do:

def foo(bar, *, baz='qux', **kwargs): 
    pass

or in other words, why python does not count **kwargs as named arguments?

Meysam Sadeghi
  • 1,483
  • 2
  • 17
  • 23
  • 2
    wouldn't that be the same as `def foo(**kwargs):`? – FObersteiner Aug 05 '20 at 09:41
  • @MrFuppes does the revision make it clear? – Meysam Sadeghi Aug 05 '20 at 10:08
  • 1
    Yes, I think you have a point there. Also, I don't think the dupe-flagged question explains this. – FObersteiner Aug 05 '20 at 10:35
  • @MrFuppes, I also think the duplicate flag is not answering my question, but I do not think that I can do anything about that! – Meysam Sadeghi Aug 05 '20 at 10:40
  • See [PEP 3102 -- Keyword-Only Arguments](https://www.python.org/dev/peps/pep-3102/#:~:text=Since%20Python%20requires%20that%20all,must%20be%20supplied%20via%20keyword.) – juanpa.arrivillaga Aug 05 '20 at 10:45
  • 1
    @juanpa.arrivillaga: in PEP 3102, is there any explanation to why `*` cannot be followed by `**kwargs`? – FObersteiner Aug 05 '20 at 10:50
  • 1
    I'm late to the party but wouldn't `def foo(bar, *, **kwargs):` just be redundant? `*` forces the caller to only use named arguments after that point. `**kwargs` does the same thing but consumes the named arguments into `kwargs` as a `dict`. What reason would you ever have for doing the former? – Axe319 Aug 05 '20 at 15:45
  • Thanks a lot @Axe319, you have a point that address my question :D – Meysam Sadeghi Aug 06 '20 at 08:18

0 Answers0