List question

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sun Mar 23 20:32:51 EDT 2008


On Sun, 23 Mar 2008 15:55:43 -0700, John Machin wrote:

> On Mar 24, 12:19 am, Dustan <DustanGro... at gmail.com> wrote:
>> On Mar 21, 3:57 pm, Paul Rubin <http://phr...@NOSPAM.invalid> wrote:
>>
>> > From at home writes:
>> > >    if 'one' and 'two' in f:
>> > >            alist.append(f)
>>
>> > Use:
>> >      if 'one' in f and 'two'  in f: ...
>>
>> Personally, I would put parentheses around to be clearer:
>>
>> if ('one' in f) and ('two'  in f): ...
>>
>> I'm not saying to put parentheses around everything, but in the more
>> ambiguous cases, it certainly helps.
> 
> Please help me understand why this is a "more ambiguous" case.

Some people don't remember, and have no interest in forcing the people 
who read their code to memorize, the relative precedence of 'in' and 
'and', and so don't expect folks to understand whether

'one' in f and 'two' in f

means

('one' in f) and ('two' in f)

or 

('one' in (f and 'two')) in f

or something else.

This especially holds for people who have used languages where the 
precedences are less sensible than they are in Python. By memory, 
standard Pascal uses the second interpretation if you don't use 
parentheses, which is stupid, but there you go.


> To me,  alternative interpretations have extremely low scores for
> utility and likelihood:

Outside of the interactive interpreter, I prefer to know what my code 
does before I write it, rather than guess that it's unlikely to be wrong.

(Or at least *think* I know what it does, which is not always the same 
thing as knowing what it does.)



-- 
Steven



More information about the Python-list mailing list