1 > 0 == True -> False

Chris Angelico rosuav at gmail.com
Thu Jan 30 10:02:21 EST 2014


On Fri, Jan 31, 2014 at 1:49 AM, Roy Smith <roy at panix.com> wrote:
> In article <mailman.6143.1391091519.18130.python-list at python.org>,
>  Chris Angelico <rosuav at gmail.com> wrote:
>
>> On Fri, Jan 31, 2014 at 1:08 AM, Roy Smith <roy at panix.com> wrote:
>> > Better than that, do what I do.
>> >
>> > 1) Assume that you don't have the full operator precedence table
>> > memorized and just parenthesize everything.
>>
>> Or:
>>
>> 1a) Assume that you don't have the full operator precedence table
>> memorized and just look it up, for whichever language you're working
>> with today. :)
>
> It's faster to just stick in some extra parens.  Not to mention that it
> makes the code more clear for everybody reading it later.

That won't protect you from getting modulo and truncating-division mixed up. :)

> Operator precedence is a tricky thing.  In part, because it's somewhat
> arbitrary, and in part because it changes from language to language.
> Using "extra" parens to make my meaning clear (to both the compiler and
> other humans who read the code in the future) is a simple technique
> which works in all languages.

It's not arbitrary, but there are differences from language to
language. Yes, parens can help, but I would strongly advocate NOT
using them where it's utterly unambiguous:

x = (2*3)+4 # Pointless!

Whether your language works with * before + (the sane way, doing what
we expect from algebra) or purely left to right (the insane way, but
some languages do do that), the parens are superfluous. Don't use 'em!

But if you work with both PHP and any other language that has a ?:
operator, parenthesizing any nesting of them will avoid a PHP
stupidity. Not that that's really any sort of argument here, though.

ChrisA



More information about the Python-list mailing list