[Python-Dev] ',' precedence in documentation

Nick Coghlan ncoghlan at gmail.com
Mon Sep 15 11:34:14 CEST 2008


C. Titus Brown wrote:
> Hi folks,
> 
> over on the pygr list, we were recently discussing a mildly confusing
> edit I made:
> 
>    assert 'seq1' in self.db, self.db.keys() 

For those suggesting changing the syntax of the assert statement due to
this misinterpretation, what makes this any more ambiguous than doing
the same thing in a function call or tuple literal?

>>> '1' in '123', 2
(True, 2)
>>> 1 and 2, lambda: 1, 3 or 4
(2, <function <lambda> at 0xb7704a3c>, 3)

>>> x = '1' in '123', 2
>>> x
(True, 2)
>>> x = 1 and 2, lambda: 1, 3 or 4
>>> x
(2, <function <lambda> at 0xb770410c>, 3)

>>> def f(*args): print args
...
>>> f('1' in '123', 2)
(True, 2)
>>> f(1 and 2, lambda: 1, 3 or 4)
(2, <function <lambda> at 0xb770410c>, 3)

Defining ',' as an "operator" in the predecence table may be technically
incorrect (since there is no actual comma operator), but giving it an
entry indicating that it has even lower precedence than lambda would
make it clear how the above expressions are meant to be parsed (and when
parentheses are going to be needed to force the correct interpretation).

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
            http://www.boredomandlaziness.org


More information about the Python-Dev mailing list