built-in pow() vs. math.pow()

Andreas Eisele andreas.eisele at gmail.com
Sat Apr 1 11:16:46 EDT 2023


Andreas Eisele schrieb am Donnerstag, 30. März 2023 um 11:16:02 UTC+2:
> I sometimes make use of the fact that the built-in pow() function has an optional third argument for modulo calculation, which is handy when dealing with tasks from number theory, very large numbers, problems from Project Euler, etc. I was unpleasantly surprised that math.pow() does not have this feature, hence "from math import *" overwrites the built-in pow() function with a function that lacks functionality. I am wondering for the rationale of this. Does math.pow() do anything that the built-in version can not do, and if not, why is it even there? 
> Thanks in advance for any enlightening comment on this. 
> Best regards, Andreas

Thanks a lot to all of you for the insightful replies! 
I now understand why math.pow() behaves the way it does and whoever tells me I should have read the doc of the math module before using it is 100% on point. 
BTW, there is another difference: built-in pow() deals with complex arguments, while functions in math won't accept them at all.
I also agree with the warning that importing * from any module is generally a bad idea, and I normally would not do it.  But here I had written a little tool that evaluates an expression given on the command line, and in this case having math functions available without further ado is very convenient, so it seemed appropriate to make an exeption to this rule.
I ended up assigning the built-in pow function to a different name before importing the math module, which is a good way to keep both variants accessible.
Best regards, and thanks again, Andreas


More information about the Python-list mailing list