Define functions with too many arguments to abide by PEP8 standard

前端 未结 7 751
日久生厌
日久生厌 2021-02-01 00:12

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,         


        
7条回答
  •  甜味超标
    2021-02-01 00:43

    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)
    

提交回复
热议问题