What is the difference between these import operations?

I have seen many online examples with various ways to import modules. I was wondering what the difference is if it's speed, accuracy, priority or psychology.

The first and most common:

 import sys import os import socket import shutil import threading import urllib import time import zipfile 

I understand the technique, but it seems unnecessary when you can use, as I personally,

 import sys, os, socket, shutil, threading, urllib, time, zipfile 

Fewer lines, less code, less headaches, at least in my opinion. However, the third one stumps me,

 import sys, os, shutil import threading import zipfile import socket, urllib import time 

What is the purpose or purpose of this import method? I would think that it would be inconvenient to mix the first two methods, as well as clutter up. It also seems to be slower than either method, or in the worst case, slower than both.

So, how was it interesting to me, what is the difference between the three?

Is there any logic in the third, for example, an increase in speed, or is it just for looks?

+5
source share
2 answers

Functionally they do the same. This is a style preference. Many adhere to the PEP-8 style guidelines (ref: https://www.python.org/dev/peps/pep-0008/#imports ), which state that imports should be on separate lines.

+6
source

The third group groups packages that are likely to be used together. When you copy the headers (setup code) from one file to another, this method makes it easy to select the exact import set that you need for the new program.

+3
source

All Articles