If you prefer to apply this operation functionally rather than with an infix operator (the **
symbol), you can pass the base and exponent as arguments to the pow
function:
In [23]: (9**(0.5)) == pow(9, 0.5)
Out[23]: True
I am also fond of finding new uses for this Infix hack in Python although it's more of a fun aside than a heavy-duty solution. But you could effectively make your own personal symbol for this by doing the following:
class Infix:
def __init__(self, function):
self.function = function
def __ror__(self, other):
return Infix(lambda x, self=self, other=other: self.function(other, x))
def __or__(self, other):
return self.function(other)
def __rlshift__(self, other):
return Infix(lambda x, self=self, other=other: self.function(other, x))
def __rshift__(self, other):
return self.function(other)
def __call__(self, value1, value2):
return self.function(value1, value2)
root_of = Infix(lambda x,y: y**(1.0/x))
print 2 |root_of| 9
3.0