| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent e267793 commit 87d6cd3
6 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -779,26 +779,23 @@ A slash in the argument list of a function denotes that the parameters prior to | |||
| 779 | 779 | it are positional-only. Positional-only parameters are the ones without an | |
| 780 | 780 | externally-usable name. Upon calling a function that accepts positional-only | |
| 781 | 781 | parameters, arguments are mapped to parameters based solely on their position. | |
| 782 | - For example, :func:`pow` is a function that accepts positional-only parameters. | ||
| 783 | - Its documentation looks like this:: | ||
| 782 | + For example, :func:`divmod` is a function that accepts positional-only | ||
| 783 | + parameters. Its documentation looks like this:: | ||
| 784 | 784 | ||
| 785 | - >>> help(pow) | ||
| 786 | - Help on built-in function pow in module builtins: | ||
| 785 | + >>> help(divmod) | ||
| 786 | + Help on built-in function divmod in module builtins: | ||
| 787 | 787 | ||
| 788 | - pow(x, y, z=None, /) | ||
| 789 | - Equivalent to x**y (with two arguments) or x**y % z (with three arguments) | ||
| 788 | + divmod(x, y, /) | ||
| 789 | + Return the tuple (x//y, x%y). Invariant: div*y + mod == x. | ||
| 790 | 790 | ||
| 791 | - Some types, such as ints, are able to use a more efficient algorithm when | ||
| 792 | - invoked using the three argument form. | ||
| 791 | + The slash at the end of the parameter list means that both parameters are | ||
| 792 | + positional-only. Thus, calling :func:`divmod` with keyword arguments would lead | ||
| 793 | + to an error:: | ||
| 793 | 794 | ||
| 794 | - The slash at the end of the parameter list means that all three parameters are | ||
| 795 | - positional-only. Thus, calling :func:`pow` with keyword arguments would lead to | ||
| 796 | - an error:: | ||
| 797 | - | ||
| 798 | - >>> pow(x=3, y=4) | ||
| 795 | + >>> divmod(x=3, y=4) | ||
| 799 | 796 | Traceback (most recent call last): | |
| 800 | 797 | File "<stdin>", line 1, in <module> | |
| 801 | - TypeError: pow() takes no keyword arguments | ||
| 798 | + TypeError: divmod() takes no keyword arguments | ||
| 802 | 799 | ||
| 803 | 800 | ||
| 804 | 801 | Numbers and strings | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1274,11 +1274,12 @@ are always available. They are listed here in alphabetical order. | |||
| 1274 | 1274 | returns ``8364``. This is the inverse of :func:`chr`. | |
| 1275 | 1275 | ||
| 1276 | 1276 | ||
| 1277 | - .. function:: pow(x, y[, z]) | ||
| 1277 | + .. function:: pow(base, exp[, mod]) | ||
| 1278 | 1278 | ||
| 1279 | - Return *x* to the power *y*; if *z* is present, return *x* to the power *y*, | ||
| 1280 | - modulo *z* (computed more efficiently than ``pow(x, y) % z``). The two-argument | ||
| 1281 | - form ``pow(x, y)`` is equivalent to using the power operator: ``x**y``. | ||
| 1279 | + Return *base* to the power *exp*; if *mod* is present, return *base* to the | ||
| 1280 | + power *exp*, modulo *mod* (computed more efficiently than | ||
| 1281 | + ``pow(base, exp) % mod``). The two-argument form ``pow(base, exp)`` is | ||
| 1282 | + equivalent to using the power operator: ``base**exp``. | ||
| 1282 | 1283 | ||
| 1283 | 1284 | The arguments must have numeric types. With mixed operand types, the | |
| 1284 | 1285 | coercion rules for binary arithmetic operators apply. For :class:`int` | |
@@ -1287,14 +1288,15 @@ are always available. They are listed here in alphabetical order. | |||
| 1287 | 1288 | converted to float and a float result is delivered. For example, ``10**2`` | |
| 1288 | 1289 | returns ``100``, but ``10**-2`` returns ``0.01``. | |
| 1289 | 1290 | ||
| 1290 | - For :class:`int` operands *x* and *y*, if *z* is present, *z* must also be | ||
| 1291 | - of integer type and *z* must be nonzero. If *z* is present and *y* is | ||
| 1292 | - negative, *x* must be relatively prime to *z*. In that case, ``pow(inv_x, | ||
| 1293 | - -y, z)`` is returned, where *inv_x* is an inverse to *x* modulo *z*. | ||
| 1291 | + For :class:`int` operands *base* and *exp*, if *mod* is present, *mod* must | ||
| 1292 | + also be of integer type and *mod* must be nonzero. If *mod* is present and | ||
| 1293 | + *exp* is negative, *base* must be relatively prime to *mod*. In that case, | ||
| 1294 | + ``pow(inv_base, -exp, mod)`` is returned, where *inv_base* is an inverse to | ||
| 1295 | + *base* modulo *mod*. | ||
| 1294 | 1296 | ||
| 1295 | 1297 | Here's an example of computing an inverse for ``38`` modulo ``97``:: | |
| 1296 | 1298 | ||
| 1297 | - >>> pow(38, -1, 97) | ||
| 1299 | + >>> pow(38, -1, mod=97) | ||
| 1298 | 1300 | 23 | |
| 1299 | 1301 | >>> 23 * 38 % 97 == 1 | |
| 1300 | 1302 | True | |
@@ -1304,6 +1306,10 @@ are always available. They are listed here in alphabetical order. | |||
| 1304 | 1306 | the second argument to be negative, permitting computation of modular | |
| 1305 | 1307 | inverses. | |
| 1306 | 1308 | ||
| 1309 | + .. versionchanged:: 3.9 | ||
| 1310 | + Allow keyword arguments. Formerly, only positional arguments were | ||
| 1311 | + supported. | ||
| 1312 | + | ||
| 1307 | 1313 | ||
| 1308 | 1314 | .. function:: print(*objects, sep=' ', end='\\n', file=sys.stdout, flush=False) | |
| 1309 | 1315 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -19,6 +19,7 @@ | |||
| 19 | 19 | import unittest | |
| 20 | 20 | import warnings | |
| 21 | 21 | from contextlib import ExitStack | |
| 22 | + from functools import partial | ||
| 22 | 23 | from inspect import CO_COROUTINE | |
| 23 | 24 | from itertools import product | |
| 24 | 25 | from textwrap import dedent | |
@@ -1206,6 +1207,18 @@ def test_pow(self): | |||
| 1206 | 1207 | ||
| 1207 | 1208 | self.assertRaises(TypeError, pow) | |
| 1208 | 1209 | ||
| 1210 | + # Test passing in arguments as keywords. | ||
| 1211 | + self.assertEqual(pow(0, exp=0), 1) | ||
| 1212 | + self.assertEqual(pow(base=2, exp=4), 16) | ||
| 1213 | + self.assertEqual(pow(base=5, exp=2, mod=14), 11) | ||
| 1214 | + twopow = partial(pow, base=2) | ||
| 1215 | + self.assertEqual(twopow(exp=5), 32) | ||
| 1216 | + fifth_power = partial(pow, exp=5) | ||
| 1217 | + self.assertEqual(fifth_power(2), 32) | ||
| 1218 | + mod10 = partial(pow, mod=10) | ||
| 1219 | + self.assertEqual(mod10(2, 6), 4) | ||
| 1220 | + self.assertEqual(mod10(exp=6, base=2), 4) | ||
| 1221 | + | ||
| 1209 | 1222 | def test_input(self): | |
| 1210 | 1223 | self.write_testfile() | |
| 1211 | 1224 | fp = open(TESTFN, 'r') | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,2 @@ | |||
| 1 | + The arguments for the builtin pow function are more descriptive. They can now | ||
| 2 | + also be passed in as keywords. | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1796,22 +1796,22 @@ builtin_ord(PyObject *module, PyObject *c) | |||
| 1796 | 1796 | /*[clinic input] | |
| 1797 | 1797 | pow as builtin_pow | |
| 1798 | 1798 | ||
| 1799 | - x: object | ||
| 1800 | - y: object | ||
| 1801 | - z: object = None | ||
| 1802 | - / | ||
| 1799 | + base: object | ||
| 1800 | + exp: object | ||
| 1801 | + mod: object = None | ||
| 1803 | 1802 | ||
| 1804 | - Equivalent to x**y (with two arguments) or x**y % z (with three arguments) | ||
| 1803 | + Equivalent to base**exp (with two arguments) or base**exp % mod (with three arguments) | ||
| 1805 | 1804 | ||
| 1806 | 1805 | Some types, such as ints, are able to use a more efficient algorithm when | |
| 1807 | 1806 | invoked using the three argument form. | |
| 1808 | 1807 | [clinic start generated code]*/ | |
| 1809 | 1808 | ||
| 1810 | 1809 | static PyObject * | |
| 1811 | - builtin_pow_impl(PyObject *module, PyObject *x, PyObject *y, PyObject *z) | ||
| 1812 | - /*[clinic end generated code: output=50a14d5d130d404b input=653d57d38d41fc07]*/ | ||
| 1810 | + builtin_pow_impl(PyObject *module, PyObject *base, PyObject *exp, | ||
| 1811 | + PyObject *mod) | ||
| 1812 | + /*[clinic end generated code: output=3ca1538221bbf15f input=bd72d0a0ec8e5eb5]*/ | ||
| 1813 | 1813 | { | |
| 1814 | - return PyNumber_Power(x, y, z); | ||
| 1814 | + return PyNumber_Power(base, exp, mod); | ||
| 1815 | 1815 | } | |
| 1816 | 1816 | ||
| 1817 | 1817 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments