question of style

Simon Forman sajmikins at gmail.com
Fri Jul 3 16:04:21 EDT 2009


On Jul 3, 2:09 am, Lie Ryan <lie.1... at gmail.com> wrote:
> Simon Forman wrote:
> > 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.
>
> I say, you should keep the `is None` for readability and to safe guard
> against immutable optimization and/or Singleton objects. Due to an
> implementation detail, python does not guarantee that two immutable
> object will return different objects; you have:
>
> >>> a = 1
> >>> b = 1
> >>> a is b
>
> True

I probably would have kept the 'is None' for those reasons except that
this code was never meant to be used by anyone but me, and it turned
out the entire line was redundant anyway. :]



More information about the Python-list mailing list