I want to know the difference between from import.

Asked 2 years ago, Updated 2 years ago, 101 views

When you import modules, you can choose whether to import two modules or from, and I wonder what the difference is.

The code itself doesn't change anyway, but I want to know which of the two is more advantageous.(e.g. faster module calls)

Or is it just for shortening the command code?

import time

time.sleep(10)
from time import sleep

sleep(10)

python import

2022-09-20 12:34

2 Answers

Experiments have shown that module calls do not speed up

I think it's only used to shorten the command code


2022-09-20 12:34

I think it's the speed difference of import.

From ~ import ~ is a command to import only a specific module portion of the library, and import is a function to just import the entire library.

If you are simply trying to write sleep, there will be little difference in speed whether you import the time library or from sleep. However, when you need to create multiple classes or create another library from one library, importing functions that you don't need can slow you down, so you can import only the parts that are needed for optimization.


2022-09-20 12:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.