Comments by "Dr Gamma D" (@DrDeuteron) on "Class Methods, Static Methods, u0026 Instance Methods EXPLAINED in Python" video.
-
2
-
I use classmethods all the time, esp. as constructor helpers...since I never do work in __init__, that method is for setting instance attributes, and that is it. If I need to, say make the instance from a file...I am not reading a file in init, rather:
@classmethod
def fromfile(cls, filename):
with open(filename, 'r') as fsrc:
return cls(*some_function(fsrc.readlines())
and I get a new instance with the file data loaded.
For static methods, say I have a class whose attributes are different real time series, well if I want an fft or something:
@staticmethod
def rfft(x_i):
return np.fft.rfff(x_i)
the reason I put it in class is that I want the object to be able to do everything that needs to be done to it to be part of the interface. I don't want a user looking for np.ftt.rftt and doing it themselves, or making the mistake of using a full fft for complex inputs. A class should contain ALL the functions you would want to use on it,,,even if they don't depend on instance/class attributes.
1
-
1
-
1
-
1
-
1
-
1