question of style

Simon Forman sajmikins at gmail.com
Thu Jul 2 19:13:47 EDT 2009


On Jul 2, 3:57 pm, Scott David Daniels <Scott.Dani... at Acm.Org> wrote:
> Duncan Booth wrote:
> > Simon Forman <sajmik... at gmail.com> wrote:
> >> ...
> >> if self.higher is self.lower is None: return
> >> ...
> > As a matter of style however I wouldn't use the shorthand to run two 'is'
> > comparisons together, I'd write that out in full if it was actually needed
> > here.
>
> Speaking only to the style issue, when I've wanted to do something like
> that, I find:
>
>        if self.higher is None is self.lower:
>            ...
>
> more readable, by making clear they are both being compared to a
> constant, rather than compared to each other.

I was going to do it that way for aesthetics if nothing else, but in
this particular code "self.higher is self.lower" could only have been
True if they were both None, so the final "is None" was redundant and
I only included it as a "just-in-case" check in case someone someday
used the code in such a way as to assign the same object (not None) to
both self.higher and self.lower...  Totally pointless, I'm sorry to
say.  I'm glad the whole line was redundant really.

> More often, I've used code like:
>
>        if <expr1> is not None is not <expr2>:
>            ...
>
> since I am usually working on non-defaulting cases in the body.
> I find the form above simpler to read than:
>
>        if <expr1> is not None and <expr2> is not None:
>            ...
>
> I do draw the line at two, though, and with three or more I'll
> paren-up a list of parallel comparisons:
>
>        if (<expr1> is not None
>          and <expr2> is not None
>          and <expr3> is not None):
>            ...
>
> --Scott David Daniels
> Scott.Dani... at Acm.Org




More information about the Python-list mailing list