Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #48918 > unrolled thread
| Started by | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| First post | 2013-06-22 18:23 +1000 |
| Last post | 2013-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.
Re: n00b question on spacing Ben Finney <ben+python@benfinney.id.au> - 2013-06-22 18:23 +1000
| From | Ben Finney <ben+python@benfinney.id.au> |
|---|---|
| Date | 2013-06-22 18:23 +1000 |
| Subject | Re: 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
Back to top | Article view | comp.lang.python
csiph-web