[Python-Dev] Adding a conditional expression in Py3.0

BJörn Lindqvist bjourne at gmail.com
Fri Sep 30 17:25:55 CEST 2005

> > I did this for my favorite proposal, and ended up with the list shown
> > further down below.
> >
> > I think they all looks great!
> >
> The fact that so few were found in whole of the standard library does
> put the use case into question, though, no? Though I am sure more could
> be found with a more thorough scan.

There's lots of code in it that looks like this:

def __init__(self, foo = None):
    if foo is not None:
        self.foo = foo
        self.foo = self.getFooDefault()

With if/else it can be written:

def __init__(self, foo = None):
    self.foo = foo if foo is not None else self.getFooDefault()

However, I'm convinced that the latter version is less readable than
the former. Often the line becomes more than 80 characters so there's
no gain in translating it to an if-else expression because you would
still have to split the line, other times the condition is so complex
that it deserves its own line. Many opportunities for uses, but
atleast as many for abuses.

mvh Björn

More information about the Python-Dev mailing list