Should I add a trailing comma after the last argument in a function call?

前端 未结 5 890
慢半拍i
慢半拍i 2020-12-15 16:15

What is better to do?

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

or

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

And what in the following

5条回答
  •  一生所求
    2020-12-15 16:38

    In data structures, the trailing comma is "useful" for making it easier to add items:

    a = [
          1,
          2,
          3,
        ]
    

    is easier to change into

    a = [
          1,
          2,
          3,
          4,
          5,
        ]
    

    because you don't have to edit the line that says 3,.

    But there is no such benefit in function calls which usually don't change in length. So I would discourage the use of a trailing comma.

提交回复
热议问题