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