Py3K idea: why not drop the colon?

Steve Holden steve at holdenweb.com
Fri Nov 10 16:06:26 EST 2006


James Cunningham wrote:
> On 2006-11-10 15:24:50 -0500, Bjoern Schliessmann 
> <usenet-mail-0306.20.chr0n0ss at spamgourmet.com> said:
> 
>> Marc 'BlackJack' Rintsch wrote:
>>
>>> No it doesn't -- look again at the example given above.  It's
>>> legal syntax in Python but doesn't have the semantics implied by
>>> the example.
>> Sorry, I don't understand -- what is the difference between the
>> example as it is and the implied semantics of it?
>>
>> Regards,
>>
>>
>> Björn
> 
> Yes, I'm not sure myself.
> 
> In [1]: color = "red"
> 
> In [2]: if color == "red" or "blue" or "green":
>    ...:     print 'Works.'
>    ...:
>    ...:
> Works.
> 
> In [3]: if color == "blue" or "red" or "green":
>    ...:     print 'Works.'
>    ...:
>    ...:
> Works.
> 
> In [4]: if not color == "blue" or "green":
>    ...:     print 'Works.'
>    ...:
>    ...:
> Works.
> 
Try testing a little more completely:

  >>> for color in ('blue', 'red', 'green', 'yellow'):
  ...   if color == 'blue' or 'red' or 'green':
  ...     print color, "compares true"
  ...   else:
  ...     print color, "compares false"
  ...
blue compares true
red compares true
green compares true
yellow compares true
  >>>

Still think it works?

  >>> print 'yellow' == 'blue' or 'red' or 'green'
red
  >>> print 'blue' == 'blue' or 'red' or 'green'
True
  >>>

Now do you understand why it doesn't work? Think "operator precedence".

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd          http://www.holdenweb.com
Skype: holdenweb       http://holdenweb.blogspot.com
Recent Ramblings     http://del.icio.us/steve.holden




More information about the Python-list mailing list