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

Raymond Hettinger raymond.hettinger at verizon.net
Thu Sep 29 14:26:34 CEST 2005

> I'm also opposed to elif in conditional expressions -- let's keep this
> simple Pythonic rewrite of C's ternary.
> I'm +0 on requiring parentheses precisely because they're annoying.
> still expecting simple boolean expressions to be the primary use case,
> and my hunch is that overall Python code will read better with the
> ternary looking cluttered.

FWIW, I scanned the standard library for all the and/or pairings where a
conditional expression was applicable.  This sampling can serve as a
reference for what "typical" uses would look like in real Python code as
created by a number of different authors.

It only takes about five minutes to try out a given syntax proposal on
all the fragments listed below.  That short exercise provides an
excellent insight into the look and feel of each proposal in real world



cgitb.py:         file = file and os.path.abspath(file) or '?'
cgitb.py:         formatter = (self.format=="html") and html or text
compileal1.py:                 cfile = fullname + (__debug__ and 'c' or
csv.py:                            (quotes[a] > quotes[b]) and a or b,
csv.py:                            (delims[a] > delims[b]) and a or b,
csv.py:                     modes[char] = reduce(lambda a, b: a[1] >
b[1] and a or b,
DocXMLRPCServer.py:         anchor = (cl and cl.__name__ or '') + '-' +
fileinput.py:                                    isfirstline() and "*"
or "", line)
formatter.py:             self.writer.send_paragraph((blankline and 1)
or 0)
gettext.py:         __builtin__.__dict__['_'] = unicode and
self.ugettext or self.gettext
imaplib.py:             l = map(lambda x:'%s: "%s"' % (x[0], x[1][0] and
'" "'.join(x[1]) or ''), l)
imputil.py: _suffix_char = __debug__ and 'c' or 'o'
keyword.py:     iptfile = args and args[0] or "Python/graminit.c"
pickle.py:             self.write(obj and NEWTRUE or NEWFALSE)
pickle.py:             self.write(obj and TRUE or FALSE)
pickletools.py:                                  pos is None and
"<unknown>" or pos,
py_compile.py:         cfile = file + (__debug__ and 'c' or 'o')
pydoc.py:     return result and re.sub('^ *\n', '', rstrip(result)) or
pydoc.py:         anchor = (cl and cl.__name__ or '') + '-' + name
pydoc.py:         lhs = name and '<strong>%s</strong> = ' % name or ''
pydoc.py:         contents = doc and [doc + '\n'] or []
pydoc.py:             line = (name and name + ' = ' or '') + repr
pydoc.py:         line = (name and self.bold(name) + ' = ' or '') + repr
pydoc.py:             host = (sys.platform == 'mac') and '' or
pydoc.py:             font = ('helvetica', sys.platform == 'win32' and 8
or 10)
robotp~1.py:         return (self.allowance and "Allow" or
"Disallow")+": "+self.path
telnet~1.py:                             cmd == DO and 'DO' or 'DONT',
telnet~1.py:                             cmd == WILL and 'WILL' or
'WONT', ord(opt))
thread~1.py:                    n!=1 and "s" or "")
token.py:     inFileName = args and args[0] or "Include/token.h"
tokenize.py:                     yield (parenlev > 0 and NL or NEWLINE,
unittest.py:         return doc and doc.split("\n")[0].strip() or None
unittest.py:         return doc and doc.split("\n")[0].strip() or None
unittest.py:                             (run, run != 1 and "s" or "",
urllib.py:             safe_map[c] = (c in safe) and c or ('%%%02X' % i)
urllib2.py:             type = file and 'I' or 'D'
xdrlib.py:                 print pred(x) and 'succeeded' or 'failed',
':', x
xmlrpclib.py:             write(value and "1" or "0")

More information about the Python-Dev mailing list