[Python-Dev] Adding a conditional expression in Py3.0 (original) (raw)
Raymond Hettinger raymond.hettinger at verizon.net
Thu Sep 29 14:26:34 CEST 2005
- Previous message: [Python-Dev] Adding a conditional expression in Py3.0
- Next message: [Python-Dev] Adding a conditional expression in Py3.0
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
[Aahz]
I'm also opposed to elif in conditional expressions -- let's keep this a simple Pythonic rewrite of C's ternary.
I'm +0 on requiring parentheses precisely because they're annoying. I'm 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 code.
Raymond
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
'o')
csv.py: (quotes[a] > quotes[b]) and a or b,
quotes.keys())
csv.py: (delims[a] > delims[b]) and a or b,
delims.keys())
csv.py: modes[char] = reduce(lambda a, b: a[1] >
b[1] and a or b,
DocXMLRPCServer.py: anchor = (cl and cl.name or '') + '-' +
name
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
"" 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 '%s = ' % 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 '127.0.0.1' or
'localhost'
pydoc.py: font = ('helvetica', sys.platform == 'win32' and 8
or 10)
robotp1.py: return (self.allowance and "Allow" or
"Disallow")+": "+self.path
telnet1.py: cmd == DO and 'DO' or 'DONT',
ord(opt))
telnet1.py: cmd == WILL and 'WILL' or
'WONT', ord(opt))
thread1.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 "",
timeTaken))
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")
- Previous message: [Python-Dev] Adding a conditional expression in Py3.0
- Next message: [Python-Dev] Adding a conditional expression in Py3.0
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]