[Python-Dev] Propose rejection of PEP 303 -- Extend divmod() for Multiple Divisors

Raymond Hettinger raymond.hettinger at verizon.net
Fri Jun 17 11:57:22 CEST 2005


This PEP has been open for two and half years without generating
discussion or support.

Its primary case (converting cumulative seconds into a tuple days,
hours, minutes, and seconds) is a bit wanting because it doesn't
generalize to months and years.  That need is already met in a more
robust and flexible way by date and time specific modules.

The use case is also somewhat unique.  Recalling 25 years of
programming, almost every real case of repeated divmod() calls have been
in a loop with a single constant denominator (i.e. dividing by a base in
a radix conversion).  The PEP does suggest other applications but they
are all narrow offerings (gallon/quart/pint/ounce, miles/yards/feet,
pound/shilling/pence) that are extremely rare in real apps.

More importantly, the gain in succinctness is offset by a loss of
clarity.  Consider:

   dist, inches = divmod(dist, 12)
   yards, feet = divmod(dist, 3)

versus a proposed:

   yards, feet, inches = divmod(dist, 3, 12)

The latter form makes it difficult to visually confirm the correct
number of target variables.  Likewise, it is not at all obvious that the
order of the 3 and 12 are correct.  Users from other languages will tend
to automatically understand the current form and be mystified by the
second (especially given how infrequently it will arise).

The PEP draws its inspiration from an APL operator.  APL imbues many of
its operators with scalar/scalar, scalar/array, and array/array
capabilities.  But in Python (not numeric) we tend to leave the
operators in simple form and abstract the vectorization into operator
independent primitives (i.e. map and reduce).  Mathematica takes a
similar approach by offering functions like NestList().  So, instead of
a vectorized divmod(), it is wiser for someone to post a single
accumulation recipe that would work with a variety of binary operators.


Executive summary:  cute, but unpersuasive and unnecessary, not worth
the time to code, test, document, maintain, and explain.



Raymond 



More information about the Python-Dev mailing list