A desperate lunge for on-topic-ness

Ben Finney ben+python at benfinney.id.au
Thu Oct 18 20:33:55 EDT 2012


Hans Mulder <hansmu at xs4all.nl> writes:

> On 18/10/12 08:31:51, Steven D'Aprano wrote:
> > some_variable = spam('x') + ham(
> >                     some_longer_variables, here_and_here, 
> >                     and_here_also)

The indentation level for continuation lines shouldn't be dependent on
the content of the first line of the statement. That leads to either
pointless fiddling with the continuation lines when one line changes; or
to a large indentation which is also pointless because it no longer
matches the first line.

> I would spell that as:
>
> some_variable = spam('x') + ham(
>     some_longer_variables,
>     here_and_here,
>     and_here_also,
> )

I dislike the indentation of the final line, because it pops out like
the start of a new statement. Either::

    some_variable = spam('x') + ham(
            some_longer_variables,
            here_and_here,
            and_here_also)
    do_next_thing()

Or::

    some_variable = spam('x') + ham(
            some_longer_variables,
            here_and_here,
            and_here_also,
            )
    do_next_thing()

depending on whether I deem it likely that new items will later be added
within the parentheses.

> > I know PEP 8 says I should drop the final round bracket to the next
> > line, but I don't normally like that.
>
> I normally put the final bracket on the next line, where it is
> very visible.  Compare:
>
> if looks_like_it_might_be_spam(
>     some_longer_variables,
>     here_and_here, and_here_also):
>     logger.notice("might be spam")
>     move_to_spam_folder(some_longer_variables)
>     update_spam_statistics(here_and_here)

To avoid this problem, I advocate 8-column indentation for continuation lines
to contrast with the 4-column indentation for a code block::

    if looks_like_it_might_be_spam(
            some_longer_variables,
            here_and_here, and_here_also):
        logger.notice("might be spam")
        move_to_spam_folder(some_longer_variables)
        update_spam_statistics(here_and_here)

>
> vs.
>
> if looks_like_it_might_be_spam(
>     some_longer_variables,
>     here_and_here,
>     and_here_also,
> ):
>     logger.notice("might be spam")
>     move_to_spam_folder(some_longer_variables)
>     update_spam_statistics(here_and_here)
>
> Which one would you say is more readable?

Mine :-)

-- 
 \       “When I get new information, I change my position. What, sir, |
  `\             do you do with new information?” —John Maynard Keynes |
_o__)                                                                  |
Ben Finney




More information about the Python-list mailing list