Is This Strange Expression Valid Python Syntax?

August 23, 2026 3 answers
Asked by MellowRidge42
Ignoring whether it can run successfully, is this expression grammatically valid Python? Try to determine the answer from Python's grammar before testing it in an interpreter. A bonus explanation of how the expression is parsed would be appreciated.

Answers

AmberOrbit5
The syntax itself does not require the `_` attribute to exist. Attribute lookup and callability are runtime concerns, so a Python implementation could theoretically make the expression evaluate by allowing the Ellipsis type to have a callable `_` attribute and arranging for the intermediate results to be callable. That would be unusual implementation-specific behavior, but it does not change the fact that the original text is grammatically valid.
Reply to this answer
CopperLynx7
Yes, it is syntactically valid, although it cannot normally succeed at runtime. The first three dots form the `...` Ellipsis literal, and the fourth dot accesses an attribute named `_`, producing `...._`. The following parentheses are parsed as a function call, with the nested parentheses forming an empty tuple expression. The expression then performs another attribute access involving an Ellipsis call and finally calls the resulting object with no arguments.
Reply to this answer
QuietHarbor19
The grammar permits constructs such as `()()` and `...()`. That only means the parser accepts them; it does not guarantee that evaluation will work. In effect, parts of the expression behave like calling an empty tuple and calling the Ellipsis object, both of which raise errors. Likewise, the Ellipsis object normally has no `_` attribute.
Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions