Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between these import statements?

I have seen many online examples with different ways of importing modules. I was wondering what the difference is, if it is in speed, accuracy, priority, or psychology.

The first, and most common is;

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

I understand the methodry, but this seems unnecessary when you can use, like I personally do;

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

Less 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 point or purpose of that method of importing? I would think that it would be inconvenient to mix the first two methods, as well as cluttered. It also seems like it would be slower than either method, or in worst case scenario, slower than both combined.

So, like I was wondering, what's the difference between the three?

Is there any logic in the third one, like a speed increase, or is it just for looks?

like image 390
x otikoruk x Avatar asked Sep 14 '15 23:09

x otikoruk x


1 Answers

Functionally, they do the same thing. It's a style preference. Many people 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.

like image 128
Zack Tanner Avatar answered Nov 08 '22 05:11

Zack Tanner