I'm new to programming and can't quite grasp the formalism of classes. I have a file plots.py consisting of a class of functions:
import numpy as np
class SHO1D:
def prob(T, x, N):
i = x+1
product = 1
while i <= N:
product *= (1 - np.exp(-1/T)**i)
i += 1
return np.exp(-1/T)**x * product
def bec(T, N):
n_0 = 0.
for x in range(N + 1):
n_0 += SHO1D.prob(T, x, N) * x
return n_0
I'm successfully importing this class in another file as follows:
from plots import SHO1D
N = 100
temps = np.logspace(1,1.45,num=300)
plt.plot(temps, SHO1D.bec(temps, N))
However, I've read in a number of posts that, when calling functions within a class, I shouldn't prefix my functions with their class name as I did above. Rather, I should make use of self
. I've not been able to modify my program above to make use of this feature without redefining my function bec
to include a third argument "self" in addition to "T" and "N", which I would like to avoid because I want to be able to plot those functions. As such, I would appreciate an explanation as to how this feature should be implemented in my case.