1

Look, I have seen many of the answers provided on this site that deal with aspects of my question. The [aforementioned] answers usually provide already existing examples that are too basic to be helpful--at least for me. But hey, maybe I'm over-complicating it.

Here is the original long-line:

    for i in range(2, l + 1):
        job_count_array["//form[@id='SubAvailSelectForm']/font/table[2]/tbody/tr[%d]/td[1]/small" % i] = sel.get_text("//form[@id='SubAvailSelectForm']/font/table[2]/tbody/tr[%d]/td[1]/small" % i)

Here is my attempt at implementing the 'long-line continuation' etiquette (as outlined by a pycon handout from 2007 that I found here):

    for i in range(2, l + 1):
        job_count_array["//form[@id='SubAvailSelectForm']/font/table[2]/ \
                        tbody/tr[%d]/td[1]/small" % i] = sel.get_text("/ \
                        /form[@id='SubAvailSelectForm']/font/table[2]/tb \
                        ody/tr[%d]/td[1]/small" % i)

Will my attempt (a) be interpreted correctly by the parser and/or (b) be made any more 'pretty' or efficient by any helpful contributers? Thank you.

Prashant Kumar
  • 20,069
  • 14
  • 47
  • 63
philthy
  • 33
  • 7

1 Answers1

4

I would go with either of the following two choices:

for i in range(2, l + 1):
    replace_index = "//form[@id='SubAvailSelectForm']/font/table[2]/tbody/tr[%d]/td[1]/small" % (i,)
    job_count_array[replace_index] = sel.get_text(replace_index)

Firstly, you have the same long string used twice, so use replace_index in its place. This makes the code shorter, and guarantees you don't have any small, hard-to-spot typos differing between the two.

for i in range(2, l + 1):
    replace_index = ("//form[@id='SubAvailSelectForm']/"
                     "font/table[2]/tbody/tr[%d]/td[1]/small") % (i,)
    job_count_array[replace_index] = sel.get_text(replace_index)

Second, you can use the "automatic concatenation of strings inside parentheses" trick instead of the end of line escaping for strings. I find it much easier to read this way.

Also worth noting is using (%i,) instead of %i for the string formatting. It has proven beneficial in my past to set up a single string formatting argument into a tuple so it is easier to add additional arguments in the future, plus it is nice to be consistent in how string formatting arguments are presented.

sberry
  • 128,281
  • 18
  • 138
  • 165
  • Concenating strings instead of using one long one spanned across several lines also avoids a potential bug - the long string *includes* all of the white space used to line up each line with the first (just not the newlines, unless you use a triple quoted string instead of escapes), while the concatenated one has the same contents as if you put it all on one line. – lvc Dec 04 '13 at 05:25
  • 1
    I find the new format syntax also helps with keeping lines neat. `foo = 'hello world {}'` then later on `print(foo.format(42))` – Burhan Khalid Dec 04 '13 at 05:35