1
0
mirror of https://github.com/satwikkansal/wtfpython synced 2024-11-25 20:44:24 +01:00

Add example: The surprising comma

Closes https://github.com/satwikkansal/wtfpython/issues/1
This commit is contained in:
Satwik Kansal 2017-09-06 16:40:38 +05:30
parent cc3eb36f92
commit be98d88440

32
README.md vendored
View File

@ -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 "<stdin>", line 1
def h(x, **kwargs,):
^
SyntaxError: invalid syntax
>>> def h(*args,):
File "<stdin>", 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? ### For what?
Suggested by @MittalAshok in [this](https://github.com/satwikkansal/wtfpython/issues/23) issue. Suggested by @MittalAshok in [this](https://github.com/satwikkansal/wtfpython/issues/23) issue.