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.
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.
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.
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.
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"
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With