n00b question on spacing

Ray Cote rgacote at appropriatesolutions.com
Fri Jun 21 17:48:54 EDT 2013


----- Original Message -----

> From: "Yves S. Garret" <yoursurrogategod at gmail.com>
> To: python-list at python.org
> Sent: Friday, June 21, 2013 5:17:28 PM
> Subject: n00b question on spacing

> Hi, I have a question about breaking up really long lines of code in
> Python.

> 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)

> Given the fact that it goes off very far to the right on my screen is
> not terribly
> pleasing to my eyes (and can be rude for other developers).

> I was thinking of splitting it up like so:
> 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?

> --
> http://mail.python.org/mailman/listinfo/python-list

Hi Yves: 
PEP8 is your definitive guide for style questions: 
<http://www.python.org/dev/peps/pep-0008/> 

and this is an interesting set of notes: 
<http://stackoverflow.com/questions/5931297/how-would-you-properly-break-this-line-to-match-pep8-rules> 

Basic rule is to break within parenthesis -- after that it becomes a matter of personal taste/style. 

In your specific example, I would do something like: 
log.msg( 
"Item wrote to MongoDB database %s %s" % ( 
settings['MONGODB_DB'], 
settings['MONGODB_COLLECTION]), 
level=log.DEBUG, 
spider=spider) 

Though you might want to: 
a) start your string right after the log.msg( 
b) put more than one settings on the same line 
c) put the last two parameters on the same line. 

I find that once I start breaking up lines for length, that I prefer to break up everything. 

Also remember when entering long lines of text that strings concatenate within parenthesis. 
So, 
("a, b, c" 
"d, e, f" 
"g, h, i") 

Is the same as ("a, b, cd, e, fg, h, i") 

--Ray 

-- 

Ray Cote, President 
Appropriate Solutions, Inc. 
We Build Software 
603.924.6079 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130621/410a38af/attachment.html>


More information about the Python-list mailing list