I have defined a function with a long list of arguments. The total characters in definition is above 80 and doesn\'t abide by PEP8.
def my_function(argument_one,
Personally I also used to come up with the same solution as @BrenBarn 's second style. I like its way to properly represent the indentation of function parameters AND its implementation, albeit that "unhappy face" is somewhat unusual to some other people.
Nowadays, PEP8 specifically gives an example for such case, so perhaps the mainstream is going to adapt that style:
# Add 4 spaces (an extra level of indentation)
# to distinguish arguments from the rest.
def long_function_name(
var_one, var_two, var_three,
var_four):
print(var_one)
We can of course go one step further, by separating each parameter into its own line, so that any future addition/deletion of parameter would give a clean git diff
:
# Add 4 spaces (an extra level of indentation)
# to distinguish arguments from the rest.
def long_function_name( # NOTE: There should be NO parameter here
var_one,
var_two,
var_three,
var_four, # NOTE: You can still have a comma at the last line
): # NOTE: Here it aligns with other parameters, but you could align it with the "def"
print(var_one)
def my_function(argument_one, argument_two, argument_three,
argument_four, argument_five):
Even though it makes the function more verbose, for more than one argument, I think this is best — the example below is from Python
—:
def my_function(
argument_one,
argument_two,
argument_three,
argument_four,
argument_five,
):
...
git diff
s, since changing one variable will only show that change. If you have more than one argument on each line, it's gonna be more visually annoying later.
git diff
later.JavaScript
and Dart
.A good convention is better than a bad one, but it's much more important to enforce one than be unnecessarily picky about them.
Once you've decided to use a standard, share your decision with your colleagues and use an automated formatter — for example, Prettier is a popular choice for JavaScript
in VS Code
; and the Dart
language has standardized one globally: dartfmt — to enforce it consistently, diminishing the need of manual editing.
I find myself this way to be quite interesting:
def my_function(
argument_one, argument_two, argument_three,
argument_four, argument_five
):
...
it allows code-folding to reveal the function signatures quite easily, for instance, consider the below snippet:
def my_function(
argument_one, argument_two, argument_three,
argument_four, argument_five
):
s1 = 1
s2 = 2
if s1 + s2:
s3 = 3
def my_other_function(argument_one, argument_two, argument_three):
s1 = 1
s2 = 2
if s1 + s2:
s3 = 3
This way allows to code-folding the whole file and seeing all functions/signatures at once, ie:
For Python code that uses type annotations, I suggest this:
def some_func(
foo: str,
bar: str = 'default_string',
qux: Optional[str] = None,
qui: Optional[int] = None,
) -> List[str]:
"""
This is an example function.
"""
print(foo)
...
If you use yapf you can use these options in .style.yapf
:
[style]
dedent_closing_brackets = true
split_arguments_when_comma_terminated = true
I personally like to line up the params one-per-line starting with the open parentheses and keeping that indent. flake8
seems happy with it too.
def guess_device_type(device_name: str,
username: str=app.config['KEY_TACACS_USER'],
password: str=app.config['KEY_TACACS_PASS'],
command: str='show version') -> str:
"""Get a device_type string for netmiko"""