It does somewhat slowly, though. A benchmark:
40.99 ± 0.15 ns q, r = x // y, x % y
40.92 ± 0.09 ns x // y; x % y
68.08 ± 0.30 ns q, r = divmod(x, y)
66.14 ± 0.42 ns q, r = dm(x, y)
Python: 3.11.4 (main, Sep 9 2023, 15:09:21) [GCC 13.2.1 20230801]
Edit: Hmm, part of the slowness is from creating the tuple, which the operator would likely also do. So how fast would the operator be? I don’t see a way to find out other than implementing it…
Benchmark script
from timeit import timeit
from statistics import mean, stdev
import sys
setup = 'x, y, dm = 42, 23, divmod'
codes = [
'q, r = x // y, x % y',
'x // y; x % y',
'q, r = divmod(x, y)',
'q, r = dm(x, y)',
]
times = {c: [] for c in codes}
def stats(c):
ts = [t * 1e9 for t in sorted(times[c])[:5]]
return f'{mean(ts):6.2f} ± {stdev(ts):5.2f} ns '
for _ in range(100):
for c in codes:
t = timeit(';'.join([c] * 100), setup, number=10**3) / 1e5
times[c].append(t)
for c in codes:
print(stats(c), c)
print('\nPython:', sys.version)
Attempt This Online!
I’d prefer /%
or //%
though, to indicate both things it does and the order of the results:
quotient, remainder = value /% divider
quotient, remainder = value //% divider
About usefulness, @mdickinson wrote about divmod
a while ago.