Reputation: 183
import numpy as np
class Y:
def __init__(self):
return None
def f(self,x):
return x
def g(self,x):
return f(x)**2
y=Y()
print y.g(3)
I know the above code will give error, but somehow I want to do the following, is there a modification to do?
Upvotes: 0
Views: 203
Reputation: 1946
Yup. It just takes a simple change. Since f(x)
is a method, you need to call it on some object. What you want here is to call it on yourself, so very simply that line becomes:
return self.f(x)**2
Upvotes: 1
Reputation: 49320
The only reason it doesn't work is because you have f(x)**2
instead of self.f(x)**2
. Make that change and it'll work perfectly.
Upvotes: 3