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

Carel Fellinger carel.fellinger at chello.nl
Fri Feb 7 22:38:13 EST 2003


On Sat, Feb 08, 2003 at 01:29:25AM +0000, Andrew Koenig wrote:
> Carel> The proposal as is looks reasonable to me, it would clean up
> Carel> those few uses of (a and b or c) that I have in my code.
> 
> Would you mind showing us some concrete examples?  I think that
> some real code examples would be quite useful to the discussion.

Bumbers, it has been quit some time since I used that construct, and
it's not as easily spotted as C's ternary operator either.  I've found
one example, others are made up.

real example:
class Query:
    ...
    def add(self, *dates):
        for date in dates:
            if type(date) == type(1): # een absolute date
                self.query.append("(cf-dag %d)" % date)
            elif len(date) == 4:      # een chinese datum
                self.query.append(
                    "(cf-dag (calendar-absolute-from-chinese '(%d %d %g %d)))"
                    % date)
            else:                     # een westerse datum
                kind = (self.reform == None or date < self.reform) and \
                       "julian" or "gregorian"
                self.query.append(#LETOP: amerikaanse volgorde maand dag, jaar
                    "(cf-dag (calendar-absolute-from-%s '(%d %g %d)))"
                    % (kind, date[1], date[2], date[0]))


I switch back and forth between an if statement and the above and-or
expression to asign to "kind".  The above and-or usage is espescially
ugly as the condition itself has an or clause too.  The if statement
is a bit to verbose to my likings here:

            else:                     # een westerse datum
                if (self.reform == None or date < self.reform):
                    kind = "julian"
                else:
                    kind = "gregorian"
                self.query.append(#LETOP: amerikaanse volgorde maand dag, jaar
                    "(cf-dag (calendar-absolute-from-%s '(%d %g %d)))"
                    % (kind, date[1], date[2], date[0]))

The following looks better (even better were it to fit on one line:)

            else:                     # een westerse datum
                kind = "julian" if (self.reform == None or date < self.reform)
                       else "gregorian"
                self.query.append(#LETOP: amerikaanse volgorde maand dag, jaar
                    "(cf-dag (calendar-absolute-from-%s '(%d %g %d)))"
                    % (kind, date[1], date[2], date[0]))

The conditional expression is long winding as it is, so I would not dare to
put it to direct use inside the self.query.append function call, hence the
temporary variable kind.


Other usage is probably like this:

class MadeUp:
   def example(self, param1=None, param2=None, param3=None, param4=None):
       # zero is a valid value for all params
       self.param1 = param1 == None and some-default1 or param1
       self.param2 = param2 == None and some-default2 or param2
       self.param3 = param3 == None and some-default3 or param3
       self.param4 = param4 == None and some-default4 or param4

Such code always waits for dreaded False-ness of some-default, but
four four-lined if-expressions in a row for such a simple thing as
dealing with default values is a lot of screen estate.  With the
new proposal it would look like:

class MadeUp:
   def example(self, param1=None, param2=None, param3=None, param4=None):
       # zero is a valid value for all params
       self.param1 = param1 if param1 != None else some-default1
       self.param2 = param2 if param2 != None else some-default2
       self.param3 = param3 if param3 != None else some-default3
       self.param4 = param4 if param4 != None else some-default4

-- 
groetjes, carel





More information about the Python-list mailing list