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


Groups > comp.lang.python > #48918

Re: n00b question on spacing

From Ben Finney <ben+python@benfinney.id.au>
Subject Re: n00b question on spacing
Date 2013-06-22 18:23 +1000
References <CAJ=2b07ETuSuo2+3Xu6vMOJA+q1JwUFTezO3LaYLG8wXd+FLBQ@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.3688.1371889399.3114.python-list@python.org> (permalink)

Show all headers | View raw


"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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: n00b question on spacing Ben Finney <ben+python@benfinney.id.au> - 2013-06-22 18:23 +1000

csiph-web