I have written a function in python which involves the use of pow with three arguments. It was working fine and then I wrote some unrelated code to the same file after which it kept telling my that pow expected two arguments but got three even though the documentation for pow says it can accept three arguments and was working fine before. I am using Python 2.7 . Anyone has any idea as to why this is happening?
Here is the function:
def fast_is_prime(p,accuracy=64):
"""
very quickly checks if a number is prime, is wrong with probability at most
1/2**(accuracy), accuracy default is 64
"""
if p == 1:
return False
return all(pow(random.randint(1,p-1),p-1,p) == 1 for i in range(accuracy))
(Credit goes to doublep and jonrsharp in the comments for figuring out the answer)
In your case the built-in function pow() was covered up the pow() from the math module. Use import math (the functions you use from it need to be prefixed with math. in this case) or use from math import X where X is the particular function you need.
Using from math import * imports all of the functions from the math module into the main namespace. Any names in the main namespace will be covered up by the names from the math module. Python Standard Library modules aren't meant to be used like this. This means that importing standard library modules like this can cover up built-in functions. For example os.open() will cover up open() if os is imported that way. Use import math or from math import X where X is the particular function you need to prevent this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With