bryan.blackbee
bryan.blackbee

Reputation: 1954

documenting imports in Python

So imagine I have a Django 1.9 application with many models.

Inside admin.py I import my models, but I want to stick to the 80 character limit. What is the best practice for something like this?

For example

from .models import app_name_student, app_name_teacher, app_name_employment, app_name_grade, app_name_subject, app_name_activity

Is this the best solution or are there better solutions that I'm not aware of? Typically, I would do this

from .models import app_name_student, app_name_teacher, app_name_employment
from .models import app_name_grade, app_name_subject, app_name_activity

Otherwise, there probably is a framework/standards that I am not aware of...

Upvotes: 1

Views: 33

Answers (1)

Burhan Khalid
Burhan Khalid

Reputation: 174622

Although there is nothing wrong with what you have - you can and should split up imports.

However, as per pep8 (the Python style guide) you can use ( ):

The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation.

That would result in code like:

from .models import (
   app_name_student,
   app_name_teacher,
   ...
   ...
)

Although it may raise a a few eyebrows as its not a common use of ( ); most developers prefer multiple import lines.

Avoid the temptation to do from .models import *.

Upvotes: 4

Related Questions