[Python-Dev] Propose rejection of PEP 303 -- Extend divmod() for Multiple Divisors
Nick Coghlan
ncoghlan at gmail.com
Fri Jun 17 12:54:22 CEST 2005
Raymond Hettinger wrote:
> Executive summary: cute, but unpersuasive and unnecessary, not worth
> the time to code, test, document, maintain, and explain.
Plus, it fails the "not every 3-line function has to be a builtin"
guideline:
def extended_divmod(numerator, *denominators):
remainders = []
for denominator in reversed(denominators):
numerator, remainder = divmod(numerator, denominator)
remainders.insert(0, remainder)
return tuple(remainders)
OK, 5 lines. Anyway, not very hard to write for anyone with a genuine
use case - and, like you, I've never used divmod for anything other
than extracting digits (or groups of digits) from numbers.
I also don't buy the 'tedious and easy to get wrong each time you need
it' justification in the PEP. Getting the argument order to the
extended divmod wrong seems to be even easier.
For each of the cited use cases, a well-named function, or a proper
class seems like a much cleaner solution.
e.g.
class Declination(object):
def __init__(self, value):
try:
# Copy a duck-typed declination
self.degrees = value.degrees
self.minutes = value.minutes
self.seconds = value.seconds
except AttributeError:
try:
# Allow any three-value sequence
self.degrees, self.minutes, self.seconds = value
except TypeError:
# Divide a number
value, self.seconds = divmod(value, 60)
value, self.minutes = divmod(value, 60)
value, self.degrees = divmod(value, 360)
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.blogspot.com
More information about the Python-Dev
mailing list