How would you properly break this line to match pep8 rules?

孤街醉人 提交于 2019-12-04 00:49:39

I don't think PEP8 says much about it, but I would simply go with double indentation for the parameters:

class MyForm(forms.Form):
    categories = forms.CharField(
            required=False,
            widget=forms.SelectMultiple(choices=CATEGORY_VALUE),
            label="Categories"
        )
    additional_item_ship_cost = forms.CharField(
            required=False,
            max_length=10,
            label="Additional Item Ship Cost"
        )

I agree with using double (8-column) indentation for continuation lines, since it easily distinguishes continuation from block indentation. I also want the indentation to be independent of the length of the first line; the length will change as the code is maintained, but that should not necessitate changing the continuation lines.

So don't line up continuation lines with anything on the first line; instead, use the same relative indentation for any continuation line.

Backslashes for continuation are problematic (invisible trailing whitespace can change the effect), and fortunately are almost never necessary, since Python automatically continues the statement within open bracketing syntax. Breaking function calls at the open-paren (and dicts at the open-brace, lists at the open-bracket, etc.) is what I reach for first.

So I'd do:

class MyForm(forms.Form):
    categories = forms.CharField(
            required=False,
            widget=forms.SelectMultiple(
                choices=CATEGORY_VALUE),
            label="Categories")
    additional_item_ship_cost = forms.CharField(
            required=False, max_length=10,
            label="Additional Item Ship Cost")

You already know that you can split a line inside parens at a comma. Did you know that you can always use the backslash-newline combination to split lines where you can't otherwise split them?:

class MyForm(forms.Form):
    categories = forms.CharField(required=False,
                                 widget=forms.SelectMultiple(choices=\
                                     CATEGORY_VALUE),                                               
                                 label="Categories")
    additional_item_ship_cost = forms.CharField(required=False, max_length=10,                                                      
                                                label=\
                                                "Additional Item Ship Cost")

In addition, you might not know that Python will concatenate adjacent literal strings, throwing away any whitespace between them, so the above could be rewritten as:

class MyForm(forms.Form):
    categories = forms.CharField(required=False,
                                 widget=forms.SelectMultiple(choices=CATEGORY_VALUE),                                               
                                 label=\
                                     "Categories")
    additional_item_ship_cost = forms.CharField(required=False, max_length=10,                                                      
                                                label="Additional"\
                                                    " Item Ship Cost")

Finally, inside parens, you can split lines at a 'dot' just like you can at a comma, and you can use parens JUST to gain this ability:

class MyForm(forms.Form):
    categories = forms.CharField(required=False,
                                 widget=forms.
                                     SelectMultiple(choices=\
                                     CATEGORY_VALUE),                                               
                                 label="Categories")
    additional_item_ship_cost = (forms.
                                     CharField(required=False, max_length=10,                                                      
                                               label="Additional "\
                                                   "Item Ship Cost"))

Combine all of these with judicious de-indenting of subsequent split lines, and you should be able to avoid exceeding an 80-character line.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!