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


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

Re: n00b question on spacing

Started byBen Finney <ben+python@benfinney.id.au>
First post2013-06-22 18:23 +1000
Last post2013-06-22 18:23 +1000
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: n00b question on spacing Ben Finney <ben+python@benfinney.id.au> - 2013-06-22 18:23 +1000

#48918 — Re: n00b question on spacing

FromBen Finney <ben+python@benfinney.id.au>
Date2013-06-22 18:23 +1000
SubjectRe: n00b question on spacing
Message-ID<mailman.3688.1371889399.3114.python-list@python.org>
"Yves S. Garret" <yoursurrogategod@gmail.com> writes:

> I have the following line of code:
> log.msg("Item wrote to MongoDB database %s/%s" %(settings['MONGODB_DB'],
> settings['MONGODB_COLLECTION']), level=log.DEBUG, spider=spider)
[…]

> Is this ok? Are there any rules in Python when it comes to breaking up
> long lines of code?

PEP 8 is the common denominator; follow its restrictions and your code
will be a lot more readable to just about any Python programmer. So,
indent 4 columns for block structure, preferably 8 columns for
continuation lines, put spaces around binary operators, etc.

As for *where* to break long lines: I prefer the continuation lines to
be a standard indentation (4 or 8 columns), which means the indentation
doesn't need to change when the first line changes later. So I break at
an open paren, brace, bracket (‘(’, ‘{’, ‘[’) etc. and allow Python to
automatically continue the statement until that bracketing is closed.

    log.msg(
            "Item wrote to MongoDB database %s/%s"
                % (settings['MONGODB_DB'], settings['MONGODB_COLLECTION']),
            level=log.DEBUG, spider=spider)

That way, if the first line changes later, you don't need to change any
of the indentation on the continuation lines:

    logger.info(
            "Item wrote to MongoDB database %s/%s"
                % (settings['MONGODB_DB'], settings['MONGODB_COLLECTION']),
            level=log.DEBUG, spider=spider)

-- 
 \     “[W]e are still the first generation of users, and for all that |
  `\      we may have invented the net, we still don't really get it.” |
_o__)                                                   —Douglas Adams |
Ben Finney

[toc] | [standalone]


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


csiph-web