PEP 308: Pep Update

Andrew Dalke adalke at mindspring.com
Fri Feb 14 00:24:00 EST 2003


[Summary: I actually think that if the "if/else expression" is added to
Python
then the below example would be considered good style.  Two of the
4 people who responded, both pro-PEP 308, said it was a "misuse".
I find that difference to be interesting.]

I posted the example:
>   # get the 2-ple of name's value and who owns it
>   a = (
>     #  The value can be cached or in the database
>     if is_cached:
>       self._get_cache[name]
>     else:
>       self.connection.fetch(name),  # database already open when getting
> source
>
>      # The ownership is stored in the database (if the
>      # name starts with "DB") or from the web service
>      if x.startswith("DB")
>        self.connection.get_owner(name)
>      else:
>        self.webservice.get_owner_by_name(name)
>   )

Several people replied:

Evan Simpson:
> ... use two temporary names [if you don't like it]
> ... This is another reason why I keep promoting mandatory parens

Steven Taschuk:
> How would you feel about [example using two if/else expressions, one for
> each term, rather then one large one]

Actually, I prefer it written as it is above.  The if/else fields on
individual
lines make it easy to see what's going on, and make it easy to allow
comments in the right places.  The example you gave loses at least one
of the comments.

Erik Max Francis:
> Any syntax can be abused.  This is no exception.

(I'm taking this as an affirmation that the above is an abuse.
That would mean the "This" in this given context refers to
"This example of syntax abuse".  It could also be that Erik
Max Francis meant "This" to be "This syntax" in which case
it is not an affirmation.  However, Erik did not say "I find
this quite readable" so I'll assume still some slight agreement
to the idea that it is an abuse.)

James J. Besemer:
> here you have discovered yet another of the thousands of ways
> Python code is subject to "abuse" by naive or sinister coders.

Hmmm, I think I should have explained myself more.

The above example is, I think, very easy to read and understand
by me.  The readability comes in because it does use indentation
to show how the different parts of the if/else are evaluated, just
like it does in an if statement.

And I like the way an if statement reads.

So if the if/else expression is added, I believe the above will
be considered an appropriate solution -- nay, even a good one.
It is clear, it shows where each term is placed, it allows for
comments.  It would not be considered an abuse, and so I'm
surprised that the last two comments thought it to be an abuse,
especially since I said "I don't like it" and not "abuse." (Perhaps
you two by now automatically react negatively to anything I say?)

Issues of specific if/else expression syntax do not come into
play.  I could write the same code using any of the notations,
including indents and comments.  Ehh, but using the form
"if cond: f else: g" means simple-minded tools which auto-indent
based on a ":" on the previous line will still be able to help
make the above structured nicely -- in fact, smarter editers
which know this is an expression won't indent as nicely!

So *if* the ternary if/else expression is added, then the above
example would, IMHO, be the appropriate way to write that
code.

The question I have is, do you agree that my above example is
a good use of the ternary if/else expression?  Eric Max Francis
and James J. Besemer do not think so while Evan Simpson says
it is (with a tweak of the parens).  I say it is as well.

If you disagree, what's a better form?  (Besides using an if/else
statement, which I think is better.)

I, however, do not like the above solution in general.  I think
it's better to (*gasp*) force people to introduce variable names
for the different terms than to let it all roll in implicitly.  Given
my desire to hobble every pro-PEP 308 here (using the language
of some of the people against my views) and keep them from
expressing their full potential as human beings, that means that
I am still anti-PEP 308.

That's in addition to my comments about the likelihood of
the misuse of the if/else expression in most code.

                    Andrew
                    dalke at dalkescientific.com






More information about the Python-list mailing list