[Python-Dev] Adding a conditional expression in Py3.0 (original) (raw)
Jason Orendorff jason.orendorff at gmail.com
Wed Sep 21 00:28:46 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 ]
On 9/20/05, Guido wrote:
On 9/20/05, Jason Orendorff <jason.orendorff at gmail.com> wrote: > return (if q: q.popleft() else: None) > return (if q then q.popleft() else None) > return q ? q.popleft() : None > > Hmmm. Score one for ?:.
Why? Just because it's shorter?
Just a gut response to the look. The verbose forms strike me as cluttered in this particular case.
In the multiline case, it doesn't look like clutter because the if/elif/else bits line up, which fits the way Python has already trained my brain.
(Oh, and a way to decide between colon or no colon: we're not using colons in list comps and genexprs either.)
(grin) Easily fixed:
print "average weight:", avg(for c in chefs: c.weight)
rdict = dict(for k, v in D.iteritems(): v, k)
Honestly, I think I would prefer this syntax. Examples from real code, before and after:
lines = [line for line in pr.block.body
if line.logical_line.strip() != '']
lines = [for line in pr.block.body:
if line.logical_line.strip() != '':
line]
row.values = \
[line[col.start:col.end].strip() for col in columns]
row.values = \
[for col in columns: line[col.start:col.end].rstrip()]
return [p for p in self.listdir(pattern) if p.isdir()]
return [for p in self.listdir(pattern): if p.isdir(): p]
-j
- 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 ]