您如何正确地打破这条线以符合 pep8 规则?

发布于 2024-11-05 16:03:59 字数 669 浏览 3 评论 0原文

给定这个实现 Django 表单的 Python 类,您将如何正确地打破它以满足 PEP8 标准?

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")

具体来说, widget= 和 label= 参数违反了 PEP8 行长度规则。

我立即想到的是,我可以在类之外定义小部件和标签,然后在类定义中使用它们,但这感觉非常不Pythonic。

Given this Python class, implementing a Django form, how would you properly break this to meet the PEP8 standards?

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")

Specifically, the widget= and label= parameters violate the PEP8 rules for line length.

What comes to mind immediately is that I could define the widget and label outside of the class and then use them in the class definition, but that feels very un-pythonic.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泛泛之交 2024-11-12 16:03:59

我认为 PEP8 对此没有说太多,但我只想对参数使用双缩进:

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 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"
        )
孤独患者 2024-11-12 16:03:59

我同意使用双(8 列)缩进作为连续行,因为它很容易区分连续和块缩进。我还希望缩进独立于第一行的长度;长度会随着代码的维护而改变,但这不需要改变连续行。

因此,不要将后续行与第一行上的任何内容对齐;相反,对任何延续行使用相同的相对缩进

用于继续的反斜杠是有问题的(不可见的尾随空格可以改变效果),幸运的是几乎从来没有必要,因为Python自动在开括号语法中继续语句。 打破开括号处的函数调用(以及开括号处的听写、开括号处的列表等)是我首先要达到的目标。

所以我会这样做:

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")
土豪我们做朋友吧 2024-11-12 16:03:59

您已经知道可以在括号内以逗号分隔一行。你知道吗,你总是可以使用反斜杠-换行符组合来分割你无法分割的行?:

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")

此外,你可能不知道Python会连接相邻的文字字符串,
扔掉它们之间的任何空格,因此上面的内容可以重写为:

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")

最后,在括号内,您可以在“点”处分割行,就像在逗号处一样,并且您可以使用括号来获得此功能:

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"))

合并所有通过明智地取消缩进后续分割行,您应该能够避免超过 80 个字符的行。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文