Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #61767 > unrolled thread

Re: Code suggestion - List comprehension

Started byChris Angelico <rosuav@gmail.com>
First post2013-12-13 09:03 +1100
Last post2013-12-13 09:03 +1100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Code suggestion - List comprehension Chris Angelico <rosuav@gmail.com> - 2013-12-13 09:03 +1100

#61767 — Re: Code suggestion - List comprehension

FromChris Angelico <rosuav@gmail.com>
Date2013-12-13 09:03 +1100
SubjectRe: Code suggestion - List comprehension
Message-ID<mailman.4035.1386885837.18130.python-list@python.org>
On Fri, Dec 13, 2013 at 7:40 AM, Shyam Parimal Katti <spk265@nyu.edu> wrote:
> sample = ['drop table sample_table;', 'create table sample_test', '(col1
> int);', 'select col1 from', ' sample_test;']
> pure_sqls = []
> query_holder= ''
> for each_line in sample:
>     query_holder += each_line
>     if query_holder.endswith(';'):
>         pure_sqls.append(query_holder)
>         query_holder = ''

By the way, side point. It's generally considered good programming
practice to use shorter names for short-lived variables and longer
names for things that hang around. I'd spell this slightly
differently:

sample = ['drop table sample_table;', 'create table sample_test',
'(col1 int);', 'select col1 from', ' sample_test;']
pure_sqls = []
cur = ''
for line in sample:
    cur += line
    if cur.endswith(';'):  # or line.endswith, or line[-1]==';'
        pure_sqls.append(cur)
        cur = ''

The short one-token names go with the short usage; the longer name
pure_sqls outlives them. Makes it easier to figure out what's
important and what's intermediate. The name 'cur' there is debatable;
I use it all over the place as a generic accumulator for the "current"
whatever I'm working with, you might prefer to use "query" or even
"q", take your pick.

Makes no difference to the code, but might make it easier for someone
to glance over your code and figure out what it's doing.

ChrisA

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web