Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Coding style Wrapping Lines

Tags:

python

As described on http://docs.python.org/tutorial/controlflow.html :

Wrap lines so that they don’t exceed 79 characters.

Usualy i use breaklines on line 80, but sometimes i have functions that requires many arguments, as an example:

extractor.get_trigger_info(cur,con,env,family,iserver,version,login,password,prefix,proxyUser,proxyPass,proxyServer,triggerPage,triggerInfo)

So, what type of advice could be given for keep the guidelines on python coding Style ? what is the best practice for functions with many arguments ?

THanks in advance.

like image 256
thclpr Avatar asked Sep 28 '12 12:09

thclpr


1 Answers

The definitive reference for questions like these is PEP 8. PEP 8 gives you the freedom to break pretty much anywhere you want (provided you break after a binary operator and use implied line-continuation inside parenthesis). Whenever you break a line, typically the next line starts in the column after the opening parenthesis:

def func_with_lots_of_args(arg1, arg2, arg3,
                           arg4, arg5):

My personal style is to try to arrange things so that the stuff on each line after the break is roughly the same length.

def func(arg1, arg2, arg3,
         arg4, arg5, arg6,
         kwd='foobar'):

rather than:

def func(arg1, arg2, arg3,
         arg4, arg5, arg6, kwd='foobar'):

Although PEP8 doesn't really say you need to do it this way.


As a side note, if you have a function with that many positional arguments, you should probably reconsider your program design.

like image 81
mgilson Avatar answered Sep 20 '22 16:09

mgilson