Skip to content

Commit be98d88

Browse files
committed
Add example: The surprising comma
Closes satwikkansal#1
1 parent cc3eb36 commit be98d88

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

README.md

+32
Original file line numberDiff line numberDiff line change
@@ -1481,6 +1481,38 @@ tuple()
14811481

14821482
---
14831483

1484+
### The surprising comma
1485+
1486+
Suggested by @MostAwesomeDude in [this](https://github.com/satwikkansal/wtfPython/issues/1) issue.
1487+
1488+
**Output:**
1489+
```py
1490+
>>> def f(x, y,):
1491+
... print(x, y)
1492+
...
1493+
>>> def g(x=4, y=5,):
1494+
... print(x, y)
1495+
...
1496+
>>> def h(x, **kwargs,):
1497+
File "<stdin>", line 1
1498+
def h(x, **kwargs,):
1499+
^
1500+
SyntaxError: invalid syntax
1501+
>>> def h(*args,):
1502+
File "<stdin>", line 1
1503+
def h(*args,):
1504+
^
1505+
SyntaxError: invalid syntax
1506+
```
1507+
1508+
#### 💡 Explanation:
1509+
1510+
- Trailing comma is not always legal in formal parameters list of a Python function.
1511+
- 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.
1512+
- **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.
1513+
1514+
---
1515+
14841516
### For what?
14851517

14861518
Suggested by @MittalAshok in [this](https://github.com/satwikkansal/wtfpython/issues/23) issue.

0 commit comments

Comments
 (0)