n00b question on spacing
Ben Finney
ben+python at benfinney.id.au
Sat Jun 22 04:23:00 EDT 2013
"Yves S. Garret" <yoursurrogategod at 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
More information about the Python-list
mailing list