-
-
Notifications
You must be signed in to change notification settings - Fork 30.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expose meaningful keyword arguments for pow() #82418
Comments
Current signature: pow(x, y, z=None, /) Proposed signature: pow(base, exp, mod=None) Benefits:
pow(2, 5, mod=4)
squared = partial(pow, exp=2) |
Looks like a solid proposal, I especially like the clarity for the 3-argument call. Often beginners ask about why there's a third argument in pow especially when teaching RSA and number-theoretic stuff. Do you mind if I take this on Raymond? |
Actually quick question, should a similar change be made for |
I've made a PR, feel free to close it if you'd rather implement this yourself or this proposal won't be accepted :) |
You can use a lambda instead of partial: squared = lambda x: pow(x, 2) Proposed names look meaningful. But after adding support of keyword arguments please compare performance of the old and the new functions. I expect that the difference will be small, but we need to check. |
Here's a little microbenchmark, let me know if there's anything specific you'd like to see: Before
Mean +- std dev: 3.80 us +- 0.23 us
Mean +- std dev: 519 ns +- 12 ns After
Mean +- std dev: 3.80 us +- 0.26 us
Mean +- std dev: 526 ns +- 18 ns |
The proposal sounds reasonable to me.
I'd leave math.pow alone here. |
Thank you. Could you please test simpler examples like pow(2, 3)? Please use the --duplicate option. |
And pow(2.0, 3.0) please. |
Before
After
|
math.pow changes removed from PR |
Thanks Ammar |
Thank you for your contribution Ammar! Nice work! |
Isn't it a new feature? Isn't it too later to add it to 3.8? |
As noted in the checkin, this was backported with the release manager's assent. FWIW, pow() itself is an old feature, recently enhanced to support negative powers in a given modulus. When the enhancement went in, we should have done this as well. |
Thank you for the explanation Raymond and sorry for the disturb. My mistake, I had not noticed the release manager's assent. |
There seems to be a slight mixup with the built-in pow() function in Python 3.8.2. Currently, under https://docs.python.org/3/library/functions.html#pow it says:
I think this should be into "Changed in version 3.8 ... ", as pow(3,4, mod=5) actually works in Python 3.8.2. The "What’s New In Python 3.8" also needs to be changed accordingly.
This example can simply be dropped now. |
It could be, but it would be better to replace it with a different example that works. Any suggestions? |
In my original PR I changed it to divmod: https://github.com/python/cpython/pull/16302/files#diff-986275b975a33c44c0aba973362516fa |
It is hard to find a builtin which could be easy and clearly implemented in Python (it means no use of dunder methods). Maybe sorted()? def sorted(iterable, /, *, key=None, reverse=False):
"""Emulate the built in sorted() function"""
result = list(iterable)
result.sort(key=key, reverse=reverse)
return result Although I think that this use case is less important. The primary goal of the feature is mentioned at the end of the section -- easy way to implement functions which accept arbitrary keyword arguments. |
divmod() should be implemented via __divmod__ and __rdivmod__. And they should be looked up on the type, not instance. There is no easy way to express it in Python accurately. This would make the example too complex. |
I don't think that matters. The example is supposed to just serve as an illustration, it doesn't need to encode the dunder dispatch semantics. The already existing example doesn't check for a __pow__. I'd picture it just as: return x//y, x%y |
Maybe a use case in this direction: int(x, base=10). int(x='3', base=12) you get
and x needs to be a positional-only to program this yourself. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: