What do you think about idea of supporting multiple divisors in divmod()
?
So instead of
minutes, seconds = divmod(t, 60)
hours, minutes = divmod(minutes, 60)
days, hours = divmod(hours, 24)
weeks, days = divmod(days, 7)
you could write:
weeks, days, hours, minutes, seconds = divmod(t, 7, 24, 60, 60)
Sample implementation:
def new_divmod(dividend, *divisors):
if not divisors:
raise TypeError('required at least one divisor')
remainders = []
for divisor in reversed(divisors):
dividend, remainder = old_divmod(dividend, divisor)
remainders.append(remainder)
return (dividend, *remainders[::-1])
I am not sure about this idea. There are not so much use cases for it. On other hand, in these rare cases when you need to write similar code, the possibility to express it in one line would be nice.
It is also not clear which order of divisors is most natural: divmod(t, 7, 24, 60, 60)
or divmod(t, 60, 60, 24, 7)
? Big-endian or little-endian?