Behaviour of time.sleep with negative arg

Andrew Bennetts andrew-pythonlist at puzzling.org
Sun Mar 14 18:35:05 EST 2004


On Sun, Mar 14, 2004 at 03:19:53PM -0600, Jeff Epler wrote:
> On Mon, Mar 15, 2004 at 08:43:51AM +1300, Colin Brown wrote:
[...]
> >     2.    A common usage of sleep is with the difference of two values. If
> > it raises an exception on negative numbers then when one takes the
> > difference of say a timestamp and current-time (real-values), to be safe you
> > would need to put the difference in a temporary variable, check that for
> > negative and only sleep for positive values - not very elegant.
> 
> def unsurprising_sleep(d):
>     if d < 0: return
>     time.sleep(d)

Or even just:

time.sleep(max(d, 0))

-Andrew.





More information about the Python-list mailing list