For review: PEP 308 - If-then-else expression

Michael Hudson mwh at python.net
Sat Feb 8 11:15:48 EST 2003


Dale Strickland-Clark <dale at riverhall.NOTHANKS.co.uk> writes:

> I am quite surprised at the number of people who have missed important
> points in this proposal and at the amount of prejudice against
> suggestions that have already appeared in other languages.

I'm prejudiced against change, and proud of it.  Not to the extent of
opposing all change without consideration, but my default opinion of
any change is "no thanks" and the merits of any change have to
overcome that.

And then there's the fact that I really don't think this change is an
enhancement.

I went through quite a large bunch of my code code searching for 'if
'.  I've found *nothing* that I would consider improved by an
if-then-else expression.  Maybe this is just the fact that I'm so
inured to the lack of an if-then-else expression I have come to see a
limitation as a virtue -- but I don't think so.

An example:

    def get_arg(self, default=1):
        """Return any prefix argument that the user has supplied,
        returning `default' if there is None.  `default' defaults
        (groan) to 1."""
        if self.arg is None:
            return default
        else:
            return self.arg

could become

    def get_arg(self, default=1):
        """Return any prefix argument that the user has supplied,
        returning `default' if there is None.  `default' defaults
        (groan) to 1."""
        return default if self.arg is None else self.arg

Is that an improvement?  *I* certainly don't think so.  The original
has all these wonderful visual cues about what's going on (colons at
end of lines and indentation being the key two).  The second just has
a stream of name-like tokens.  It looks better when syntax hilighted,
but only slightly.

So I find myself in the slightly surprising position of favouring the
punctuation-style:

    def get_arg(self, default=1):
        """Return any prefix argument that the user has supplied,
        returning `default' if there is None.  `default' defaults
        (groan) to 1."""
        return self.arg is None ? default : self.arg

This is a 180 degree turn since I started writing this post :-)

I still don't like the proposal, though.

> I don't think anyone would describe list comprehension as Pythonic,
> yet this is perhaps the most useful recent addition to the language.

Bah.  New-style classes and generators both kick list comps' butts :-)

> The other consideration often raised and used as an excuse for an
> objection or a standpoint are thoughts for the poor beginners. 

This post is interesting reading:

http://mail.python.org/pipermail/python-dev/2003-February/032983.html

I can't really comment, as I've never tried to teach Python, but I
believe him.

[...]
> I think we can ignore those who simply flatly vote against this
> proposal. They obviously don't use the language as much as the rest
> of us or have a problem with change.

This seems unecessarily perjorative.  Anyway, I hope my vote counts in
your mind now.

Cheers,
M.

-- 
  The ultimate laziness is not using Perl.  That saves you so much
  work you wouldn't believe it if you had never tried it.
                                        -- Erik Naggum, comp.lang.lisp




More information about the Python-list mailing list