简体   繁体   中英

importing imports from your imports in python

Question: In python, Is it wise to use the imports from your sub classes, or does it matter?


Info:

So, I have a program split up over 6 files. in almost every one of the .py files i import threading, socket, and pickle. What I'm wondering is this, is there an efficiency difference between:

File1.py:

import socket

File2.py:

import File1
import socket

and this:

File2.py:

import File1
from File1 import socket

Or even this:

File2.py

import File1
socket = File1.socket

Don't use from File1 import socket . It doesn't make a performance difference, but it gives headaches to other people having to look in the other file to see that File1.socket is actually socket , and it might get messy if you decide you don't need socket in File1 .

Also, this is against the python principles because:

  1. it's not the obvious way to do it
  2. flat is better than nested
  3. readability counts
  4. special cases aren't special enough to break the rules

The import statement is smart enough to realize when a module has already been imported, an not import it again. So:

File.py:

import socket

File2.py:

import File1
import socket

is just fine.

No, there is no efficiency difference. The first approach is the best, since it promotes modularity (like for instance, what if File1.py decides to stop importing socket , then File2.py gets broken)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM