Is there anything bad that you have several imported goods on the same line?

When I program in Python and I need to import several modules, I usually like the following:

import random, time, matplotlib, cheese, doge 

Then, when I read the code of other people, this is what I see:

 import random import time import matplotlib import cheese import doge 

Why is this? Is there a difference between the two styles?

+6
source share
5 answers

PEP-8 (Style Guide for Python Code)

 Imports should usually be on separate lines, for eg Yes: import os import sys No: import sys, os It okay to say this though: from subprocess import Popen, PIPE 

To answer your question - both will work fine, but one does not comply with PEP8 recommendations.

+9
source

The practice of one import per line is standardized in PEP8 , and, following the generally accepted standard, there is ample reason to do what others do. Following the generally accepted standard, follow the Least Surprise Principle , which makes it easier for people to familiarize themselves with the standard and change your code.

Even if you do not care about PEP8, although one import per line makes your code more convenient to maintain.

  • Import is easier to read / read :

    • Itโ€™s easier to see that you get fred in import fred than in import barney, betty, wilma, fred, bambam, pebbles
  • Import is easier to find :

    • A search for "import fred" will find import fred and import fred, wilma, pebbles , but will not find import barney, fred
  • Import is easier to edit :

    • Most editors insert and delete an entire line quickly.
    • There is only one module in the line, so you do not need to search in the line to find what you want to edit - this is at the end.
    • Moving an import inside a module simply moves an entire line.
    • Copying one of several imports to another Python module is a copy of a row insert, instead of copy-pasting and then trimming other files you import that you don't need.
  • Import is easier to maintain :

    • Each changed module has its own line in the change set - you do not need to read the line to find out which module or modules have changed.
    • Missing and added modules affect the number of lines in the file and in the changeset.
    • Typos are easier to select and adjust the visual snapshot of a set of changes.

One import per line would be a good idea, even if it were not a standard. Since this is a standard, it is doubly better suited.

+8
source

These two examples are functionally equivalent. However, in PEP 8, the official style guide for Python, there is a section here that condemns the practice of placing multiple imports on the same line:

  Imports should usually be on separate lines, eg:

     Yes: import os
          import sys

     No: import sys, os

 It okay to say this though:

     from subprocess import Popen, PIPE

Thus, many Python programmers put only one import per line to follow this guide.

+1
source

PEP-8, the official Python style guide, requires that one package or module be imported into a string.

This is considered a good style, and overall standardization makes it easier to read programs. I donโ€™t think there are significant differences under the hood to worry if this is what you are asking for.

+1
source

I do not like to blindly follow without a good reason. Like PEP20: Zen of Python states that Reading Counts

PEP8 "separate line for import" works for a general perspective. Although I respect him (i.e. Guido's Opinion), I will not always strictly follow these conventions all the time.

An exception to this rule is only when the # code is less than the # import module. for example 2 lines of code, but 4 module imports.

This is more readable: (in my opinion)

 import os, sys, math, time def add_special(): return time.time() + math.floor(math.pow(sys.api_version + os.getpid(), 2)) 

instead of this

 import os import sys import math import time def add_special(): return time.time() + math.floor(math.pow(sys.api_version + os.getpid(), 2)) 

But this issue of readability is different for every person.

+1
source

Source: https://habr.com/ru/post/970968/


All Articles