-1

Why does the pow function exists if there is the ** operator? It's even faster than the function:

from timeit import timeit
print(timeit('pow(3000, 3000)', number=10000))
print(timeit('3000**3000', number=10000))

Output:

1.3396891819999155
1.3082993840000654
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Jaime02
  • 299
  • 7
  • 21
  • 5
    Does this answer your question? [Difference between the built-in pow() and math.pow() for floats, in Python?](https://stackoverflow.com/questions/10282674/difference-between-the-built-in-pow-and-math-pow-for-floats-in-python) – solid.py Mar 07 '20 at 17:46
  • @hitter no, what I ask is why does it exist because there is a operator which provides the same feature – Jaime02 Mar 07 '20 at 17:54
  • 3
    Click the link, read the question, and all your doubts will be `casted away in the wind`. ;) – solid.py Mar 07 '20 at 17:55
  • @hitter - I don't understand. That question is talking about the difference between `pow` and `math.pow` while this asks about `**` and `pow`. Could you gather back your wind-blown doubts long enough to explain the connection? – tdelaney Mar 07 '20 at 18:03
  • @hitter - What makes you think I just read the title? Its a long question and answer, so maybe I missed something, but it simply equates `**` and `pow` and then talks about the difference those two have with `math.pow`. Can you reference a bit of text in that answer that says why `pow` exists so that I can find my mistake? – tdelaney Mar 07 '20 at 18:09
  • As to the original question... there has always been a tension in python among programming styles. Guido [disliked map, filter and reduce](https://www.artima.com/weblogs/viewpost.jsp?thread=98196) because he thought that list comprehensions, etc..., were more clear. `pow` exists because it may be useful to pass it as a function to some other function. – tdelaney Mar 07 '20 at 18:25
  • @hitter - I didn't ask the question and am not responsible for any downvotes. I think its a good question and that the answer is that early in python somebody thought it would be handy to have a functional equivalent to the `**` operator (in the source, both call `PyNumber_Power`) in case someone wanted to pass the function object to some other function. – tdelaney Mar 07 '20 at 18:27
  • 1
    @hitter - recall as per your linked question, the `math.pow` function is not equivalent. – tdelaney Mar 07 '20 at 18:28
  • 1
    @tdelaney Ok, this is getting tiring. Look, I didn't see your username, and you asked in a condescending tone, that usually questioners have. I see you are a high rep user. That means a) you are experienced enough to find the answer and b) when and if you find it mark it as a new duplicate, if it's different from what I've marked. – solid.py Mar 07 '20 at 18:30
  • @tdelaney Also I am not obliged to answer anything, Remember I didn't write an answer, these are all comments ;) – solid.py Mar 07 '20 at 18:33
  • 2
    **NOTE**: We have 2 close votes for a dup, but it clearly is not. Its a good question and one answer is simply that `**` and `pow` are subtly different. Please don't close it unless you find a better quality dup. – tdelaney Mar 07 '20 at 19:35

1 Answers1

1

I found one use for the pow function over the ** operator. First, ** is really raise to a power and apply an optional modulus, as in a**b % c. But if you include the %, its value can't be None. 2**3 % None is an error. pow is really pow(x, y, z=None).

So, if I want to raise a derived value to a power, I could use the operator

>>> def powerizer(db, index, modulus=None):
...     x, y = db.get(index)
...     return x**y % modulus
... 
>>> db = {"foo":[9,3]}
>>> powerizer(db, "foo", 10)
9

But it fails on the default None modulus.

>>> powerizer(db, "foo")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in powerizer
TypeError: unsupported operand type(s) for %: 'int' and 'NoneType'

pow function to the rescue

>>> def powerizer(db, index, modulus=None):
...     x, y = db.get(index)
...     return pow(x, y, modulus)
... 
>>> powerizer(db, "foo", 10)
9
>>> powerizer(db, "foo")
729
tdelaney
  • 73,364
  • 6
  • 83
  • 116