1. WHAT
this is a short non-critical patch to give round()
keyword arguments for Partial Function Application
(PEP309) purposes.
2. WHY
arguments that appear on the RHS of function
declarations are hard to call when using
functional.partial because the "variable" argument is
to the left (since PFAs pretty much favor only variable
arguments to the right). if fixed arguments are to the
right (are "curried"), then we need to support keyword
args for them as much as possible to encourage their use.
3. MOTIVATION
similar to the example in the Python Lib Ref doc page
for the functional module and its use with int()...
http://docs.python.org/dev/lib/module-functional.html
>>> basetwo = partial(int, base=2)
>>> basetwo.__doc__ = 'Convert base 2 string to an int.'
>>> basetwo('10010')
18
...we should also allow round() to be used in the same
way...
>>> from functional import partial
>>>
>>> roundTwo = partial(round, ndigits=2)
>>> print '%.2f' % roundTwo(1.5555555)
1.56
>>> print '%.2f' % roundTwo(1.5545555)
1.55
>>> print '%.2f' % roundTwo(1.5445555)
1.54
without this patch, users current get this:
>>> from functional import partial
>>> roundTwo = partial(round, ndigits=2)
>>> '%.2f' % roundTwo(1.5555555)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: round() takes no keyword arguments
4. THE FIX
the number variable in bltinmodule.c is renamed from
'x' to 'number', and the keyword is named 'ndigits'
after the doc string:
>>> print round.__doc__
round(number[, ndigits]) -> floating point number
this patch consists of a single file,
Python/bltinmodule.c. the original CVS version from
Mar 28, the patched version, output from running
test/test_builtin.py, and both the C and U diff files
are included in the attached ZIP file.
5. TESTING
as mentioned above, we ran the test/test_builtin.py
script and there are no changes in the execution of
test_round:
:
test_repr (test.test_builtin.BuiltinTest) ... ok
test_round (test.test_builtin.BuiltinTest) ... ok
test_setattr (test.test_builtin.BuiltinTest) ... ok
:
the full output file is in the ZIP file.
|