SyntaxError with passing **kwargs and trailing comma

前提是你 提交于 2019-12-23 06:52:07

问题


I wonder why this is a SyntaxError in Python 3.4:

some_function(
  filename = "foobar.c",
  **kwargs,
)

It works when removing the trailing comma after **kwargs.


回答1:


As pointed out by vaultah (who for some reason didn’t bother to post an answer), this was reported on the issue tracker and has been changed since. The syntax will work fine starting with Python 3.6.

To be explicit, yes, I want to allow trailing comma even after *args or **kwds. And that's what the patch does. —Guido van Rossum




回答2:


The reason it was originally disallowed is because **kwargs was the last allowed item in an argument list -- nothing could come after it; however, a , looks like there could be more following it.

That has changed so that we can now call with multiple keyword dicts:

some_func(a, b, **c, **d,)

For consistency's sake, trailing commas are now supported in both definitions and callings of functions. This is really useful when one has either several arguments, or a few long arguments, and so the logical line is split across several physical lines.

The trailing commas are optional in both locations.



来源:https://stackoverflow.com/questions/33350454/syntaxerror-with-passing-kwargs-and-trailing-comma

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!