Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PEP 8: How should __future__ imports be grouped?

According to PEP 8:

Imports should be grouped in the following order:

  1. standard library imports
  2. related third party imports
  3. local application/library specific imports

You should put a blank line between each group of imports.

But it does not mention about __future__ imports. Should __future__ imports be grouped together with standard library imports or separated from standard library imports.

So, which is more preferred:

from __future__ import absolute_import
import sys
import os.path

from .submod import xyz

or:

from __future__ import absolute_import

import sys
import os.path

from .submod import xyz
like image 410
minhee Avatar asked May 18 '12 08:05

minhee


People also ask

What are pep 8 guidelines in Python?

PEP 8, sometimes spelled PEP8 or PEP-8, is a document that provides guidelines and best practices on how to write Python code. It was written in 2001 by Guido van Rossum, Barry Warsaw, and Nick Coghlan. The primary focus of PEP 8 is to improve the readability and consistency of Python code.

Which of the following are best practices when following the guideline in PEP 8?

PEP 8 specifies the following rules for the inline comments. Start comments with the # and single space. Use inline comments carefully. We should separate the inline comments on the same line as the statement they refer.

What is PEP 8 in Python Mcq?

Answer:- PEP 8 is a coding convention, a set of recommendations, about how to write your Python code more readable.

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 personally separate them. A __future__ import isn't just binding a name like other imports, it changes the meaning of the language. With things like from __future__ import division the module will likely run fine both with and without the import, but give different (wrong) results at places that have nothing telling me to go look at names imported if I want to know more about where they come from. __future__ imports should stand out as much as possible.

Also, I generally sort imports within a group alphabetically (no particularly good reason for doing that; I just find it has some very small benefits to diffs and merging branches), and __future__ imports have to be first, so I put them in their own group.

like image 180
Ben Avatar answered Oct 26 '22 23:10

Ben