-1

Is it the same in Python ?

import time.sleep

and

from time import sleep

Can anyone describe what is the difference between those.

Nic3500
  • 8,144
  • 10
  • 29
  • 40
Min Latt
  • 5
  • 1
  • 4

1 Answers1

0

If time.sleep is a module or a package, then import time.sleep and from time import sleep are almost the same.

Both will import time and time.sleep, but they will bind different things to local variables.

In a way, you could say that this:

from time import sleep

is more-or-less the same as this:

import time.sleep
sleep = time.sleep
del time

Because, after from time import sleep, there is no local variable time, but there is a local variable sleep.


On the other hand, if it is a class, rather than a module, there is a difference: you can do from module_name import ClassName, but you cannot import module_name.ClassName.

zvone
  • 18,045
  • 3
  • 49
  • 77