This works and happily prints 81:
class X:
mypow = pow
print(X().mypow(3, 4))
But why? Isn't the method given the extra "self" argument and should be utterly confused?
For comparison, I also tried it with my own Pow
function:
def Pow(x, y, z=None):
return x ** y
class Y:
myPow = Pow
print(Pow(3, 4))
print(Y().myPow(3, 4))
The direct function call prints 81 and the method call crashes as expected, as it does get that extra instance argument:
Python 3: TypeError: unsupported operand type(s) for ** or pow(): 'Y' and 'int'
Python 2: TypeError: unsupported operand type(s) for ** or pow(): 'instance' and 'int'
Why/how does Pythons own pow
work here? The documentation didn't help and I couldn't find the source.
Pow
? @PadraicCunningham – Pegu__self__
attribute of builtin functions is not writeable, hence it is alwaysNone
forpow
. – Bus__self__
now, didn't know that yet. – Pegu__self__
on built-in functions is not as documented – Bus