My first Python program

Ben Finney ben+python at benfinney.id.au
Wed Oct 13 20:28:21 EDT 2010


"Jonas H." <jonas at lophus.org> writes:

> On 10/13/2010 11:26 PM, Seebs wrote:
> >>      stderr.write(
> >>          "WARNING:"
> >>          " Pants on fire\n")
> >
> > Hmm.  So I just indent stuff inside the ()s or whatever?  I can work with
> > that.
>
> I think common is
>
>     stderr.write("WARNING: ",
>                  "Pants on fire")

Which makes it unwieldy if the first line changes length. Why bother
keeping lots of lines aligned every time the first line changes length?
Just use a single level of indentation for continuation lines, and the
first line can change length as much as it needs to without requiring
editing every continuation line.

> or
>
>     stderr.write(
>         "WARNING: "
>         "Pants on fire"
>     )

Better, and it allows more lines to be added between the parens without
messing around with any other lines.

In cases where the lines aren't conceptually a fungible list, though,
I'd just put the closing paren on the end of the last useful line. As a
Python programmer, we're committed to reading changes in indentation as
terminating a group of lines.

> If you haven't got braces around an expression and you want it to be
> multi-line, you need a '\' at the end of each line, just like C
> macros:
>
>     msg = "WARNING: " \
>           "Pants on fire"
>
> Though that is not commonly used afaik.

I'd go so far as to call it an anti-idiom. Better to add parens for
grouping lines as needed.

    long_identifier_to_demonstrate_my_point_again = (
        "WARNING:"
        " Pants on fire\n"
    )

-- 
 \     “Men never do evil so completely and cheerfully as when they do |
  `\        it from religious conviction.” —Blaise Pascal (1623–1662), |
_o__)                                                   Pensées, #894. |
Ben Finney



More information about the Python-list mailing list