[Python-ideas] "else if" as equivalent for "elif"

Nick Coghlan ncoghlan at gmail.com
Fri Oct 23 12:09:26 EDT 2015


On 22 October 2015 at 18:22, Sven R. Kunze <srkunze at mail.de> wrote:
> On 22.10.2015 14:19, Geoffrey Spear wrote:
>>
>> Obviously this is sometimes a cause of confusion to people new to the
>> language, when people think "oh, Python is just like English, so I should
>> use 'is' instead of '=='" or "why doesn't `if foo or bar == 'baz'` do what I
>> mean?", but for the most part having easy-to-read actual English words for
>> keywords and functions with names that actually say what they do instead of
>> making some obscure reference to a badly-named C library function written at
>> a time when limiting identifiers to 6 letters seemed like a good idea to
>> save precious disk space (or room on a punchcard), in my opinion, has
>> greatly contributed to Python's reputation as a readable language.
>
>
> So, why stop there? ;)
>
>> "elif" is a wart, but again, one I think we're stuck with.
>
>
> Dangerous thinking.
>
> I don't regard changing it to be a big deal given 10 years or so of parallel
> usage.

It's a big deal due to opportunity cost - any time that is spent on
making a second spelling of "elif" possible is time not spent working
on something that directly improves developer productivity (like
format strings) or application correctness (like gradual typing
support). Time spent on frivolous changes also (justifiably) annoys
users of the language given the large numbers of known problems we've
yet to figure out how to find the time and resources to resolve.

>From a learning perspective, remembering that "elif" is an
abbreviation of "else if" in Python isn't any more complicated than
remembering that "don't" is an abbreviation of "do not" in English.
It's certainly easier to remember than what "def" or "class" means.

>From a readability perspective, inserting an "se " in the middle of
every elif clause doesn't really improve things:

    if x == 1:
        ...
    elif x == 2:
        ...
    elif x == 3:
        ...
    else:
        ...

vs:

    if x == 1:
        ...
    else if x == 2:
        ...
    else if x == 3:
        ...
    else:
        ...

It can actually be argued that it's *harder* to read, since the
typical English phrasing for this kind of formulation is more along
the lines of "X if case A, else Y if case B, else Z", which is the way
conditional expressions are written.

By contrast, the statement form has an implied "then" after each
condition: "if case A, then X, else if case B, then Y, else Z".
Compared to dropping "then" entirely, abbreviating "else if" to "elif"
is a fairly minor change.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list