Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

pep8

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.

like image 498
Robert Roland Avatar asked May 09 '11 00:05

Robert Roland


People also ask

What is PEP 8 and why is it important?

PEP 8 is a document that provides various guidelines to write the readable in Python. PEP 8 describes how the developer can write beautiful code. It was officially written in 2001 by Guido van Rossum, Barry Warsaw, and Nick Coghlan. The main aim of PEP is to enhance the readability and consistency of code.

Should I follow PEP8?

PEP8 is for humans but your program will run even if you do not follow it. If you do not share your code and do not plan to do so, do whatever you want. If you plan to share some part of your code someday, then you should follow PEP8.

Which choice is PEP 8 compliant as the name of a class?

I try to adhere to the style guide for Python code (also known as PEP 8). Accordingly, the preferred way to name a class is using CamelCase: Almost without exception, class names use the CapWords convention.


1 Answers

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"
        )
like image 121
sth Avatar answered Oct 10 '22 21:10

sth