我应该在函数调用的最后一个参数后面添加一个尾随逗号? [英] Should I add a trailing comma after the last argument in a function call?

查看:279
本文介绍了我应该在函数调用的最后一个参数后面添加一个尾随逗号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最好做什么?

self.call(1, True, "hi")

self.call(1, True, "hi",)

p>

And what in the following cases:

self.call(
    1,
    True,
    "hi"
)

self.call(
    1,
    True,
    "hi",
)

在数据结构中添加尾随逗号的原因是我熟悉的, p>

Reasons for adding a trailing comma in data structures are familiar to me, but what about function calls?

推荐答案

我认为没有技术原因可以避免在函数调用中使用斜杠,但有些人可能会发现他们分心。有些人可能会停下来说,嗯,我想知道这真的应该在那里吗?

I think there's no technical reason to avoid trailing slashes in function calls, but some people probably do find them distracting. Some may stop and say, "Hmmm, I wonder if that's really supposed to be there?"

我不犹豫地将此称为优点,但是使用尾部斜杠连同缩进样式的一个效果是使版本控制差异

I hesitate to call this a benefit, but one effect of using trailing slashes in conjunction with an indented style is to make version control diffs look a little bit cleaner when adding an argument.

例如,这样的函数:

def my_fun(a, b, c=None):
    ...


$ b b

...这样调用:

...called like this:

my_fun(
    a='abc',
    b=123
)

...然后更改为:

my_fun(
    a='abc',
    b=123,
    c='def'
)

在git中产生此差异:

produces this diff in git:

$ git diff
...
 my_fun(
     a='abc',
-    b=123
+    b=123,
+    c='def'
 )



Whereas,

my_fun(
    a='abc',
    b=123,
)

更改为...

my_fun(
    a='abc',
    b=123,
    c='def',
)

在git中生成此差异:

produces this diff in git:

$ git diff
...
 my_fun(
     a='abc',
     b=123,
+    c='def',
 )

这篇关于我应该在函数调用的最后一个参数后面添加一个尾随逗号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆