From be98d884400bb01cafd8168fb8f5d2ba6f4f3bc8 Mon Sep 17 00:00:00 2001 From: Satwik Kansal Date: Wed, 6 Sep 2017 16:40:38 +0530 Subject: [PATCH] Add example: The surprising comma Closes https://github.com/satwikkansal/wtfpython/issues/1 --- README.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/README.md b/README.md index 5fe7066..0194a4e 100755 --- a/README.md +++ b/README.md @@ -1481,6 +1481,38 @@ tuple() --- +### The surprising comma + +Suggested by @MostAwesomeDude in [this](https://github.com/satwikkansal/wtfPython/issues/1) issue. + +**Output:** +```py +>>> def f(x, y,): +... print(x, y) +... +>>> def g(x=4, y=5,): +... print(x, y) +... +>>> def h(x, **kwargs,): + File "", line 1 + def h(x, **kwargs,): + ^ +SyntaxError: invalid syntax +>>> def h(*args,): + File "", line 1 + def h(*args,): + ^ +SyntaxError: invalid syntax +``` + +#### 💡 Explanation: + +- Trailing comma is not always legal in formal parameters list of a Python function. +- In Python, the argument list is defined partially with leading commas and partially with trailing commas. This conflict causes situations where a comma is trapped in the middle, and no rule accepts it. +- **Note:** The trailing comma problem is [fixed in Python 3.6](https://bugs.python.org/issue9232). The remarks in [this](https://bugs.python.org/issue9232#msg248399) post discuss in brief different usages of trailing commas in Python. + +--- + ### For what? Suggested by @MittalAshok in [this](https://github.com/satwikkansal/wtfpython/issues/23) issue.