Python coding style

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

Wrap the lines so that they do not exceed 79 characters.

I usually use break lines on line 80, but sometimes I have functions that require a lot of arguments:

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

So, what advice could you give to keep the Python style recommendations in mind? What is the best practice for functions with many arguments?

Extremely important.

+4
source share
2 answers

The defining step for such issues is PEP 8 . PEP 8 gives you the freedom to break anywhere wherever you want (provided that you crash after the binary operator and use the implied continuation of the line in brackets). Whenever you break a row, usually the next row starts in the column after opening the parenthesis:

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

My personal style is trying to arrange things so that the material in each line after the break is about the same length.

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

but not:

 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 many positional arguments, you should probably rethink your program design.

+5
source

I suggest:

  • save your code in two automated tools: pyflakes and pep8.py (there are plugins for a text editor for this)

  • Choose the method that you think is best and stick to this overall project. if there is a technique, stick to this.

0
source

Source: https://habr.com/ru/post/1436802/


All Articles