A developer notices that `datetime.now().microsecond` sometimes returns identical values or 'jumps' in increments larger than 1us. Why?

Python interview question for Advanced practice.

Answer

The resolution depends on the OS system clock. It may not update every microsecond, causing jitter or coarse granularity.

Explanation

Python's datetime.now() relies on the underlying operating system's clock (e.g., gettimeofday). If the OS clock resolution is low (e.g., 1ms or 15ms), Python cannot provide true microsecond precision, resulting in stepped values.

Related Questions