This works and happily prints 81:
class X: mypow = pow print(X().mypow(3, 4))
But why? Doesn't the method give an extra self argument and should be completely confused?
For comparison, I also tried this 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))
A direct function call prints 81, and the method call fails, as expected, as it receives an additional 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 Pow here? The documentation did not help, and I could not find the source.
python methods self
Stefan pochmann
source share