I have heard that using sleep()
in python is more appropriate than using time.sleep()
. If so, why and where is one more appropriate than the other one? (Is one more appropriate than the other or is it just the same? - without opinions - only fact.)
P.S. - Put sleep()
or time.sleep()
at the beginning of your answer so I know which one you have chosen.
Sleep:
from time import sleep
sleep(1)
Time sleep:
time.sleep(1)
sleep() doesn't exist, except you do
from time import sleep
if you want to use time.sleep()
you require following import
import time
at the beginning of the file
With above imports time.sleep()
and sleep()
are identical and I personally wouldn't say, that one is much more appropriate than the other.
However:
The advantage of
from time import sleep
is, that you could
replace this if you need with
from myspecialmodule import sleep
if you ever wanted to have another implementation of sleep.
and of course it is a little less typing in your code. (just sleep()
instead of time.sleep()
)
Just as a small side note 2022-08-16:
calling sleep()
would be slightly faster than calling time.sleep()
as the python byte code would have to do one less symbol lookup.
In reality (except when being within a very tight performance critical loop) this will not be noticeable.
And in this particular context where you call a function, that should wait it wouldn't make any difference