pythonimplicitpepcontinuationlong-lines

How to gracefully break up a long string


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.


Solution

  • 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.