I know there is a lot of topics on this subject, and I've looked at many. However it is still confusing to me when its appropriate to use static and class methods in place of an instance method.
In my example, I'm making a script for the autodesk program maya. One of my modules has a class in it with methods that will generate an object in it, like below:
class Curves(object):
"""Contains methods to generate curves
"""
@classmethod
def circle_crv(cls):
name = mc.circle( nr=(0, 1, 0), c=(0, 0, 0), r=0.5 )
mc.delete(name, ch=True)
mc.addAttr(name[0], ln='crvType', dt='string', h=True)
mc.setAttr(name[0]+'.crvType', 'circle_crv', typ='string', l=True)
return name[0]
However these will never be directly accessed by an external module. I run all of these methods through a dictionary thats accessed by an external modules (which first searches through an external user dictionary), which is below:
def curve_lib(self, crvtype):
"""Creates a specified curve at origin
Args:
crvtype (str): a key to call a function to generate a curve at the
origin
"""
userlib = self.usercurve_lib()
curves_dic = {
'bendjoint_crv' : self.bendjoint_crv,
'circle_crv' : self.circle_crv,
'circlearrow_crv' : self.circlearrow_crv,
'connectjoint_crv' : self.connectjoint_crv,
'fktext_crv' : self.fktext_crv,
'joint_crv' : self.joint_crv,
'iktext_crv' : self.iktext_crv,
'cube_crv' : self.cube_crv,
'quadarrow01_crv' : self.quadarrow01_crv,
'quadarrow02_crv' : self.quadarrow02_crv,
'quadarrow03_crv' : self.quadarrow03_crv,
'quadarrow04_crv' : self.quadarrow04_crv,
'rootjoint_crv' : self.rootjoint_crv,
'square_crv' : self.square_crv,
'switch_crv' : self.switch_crv,
'triangle_crv' : self.triangle_crv
}
if crvtype in userlib:
name = self.usercurve_lib(crvtype)
elif crvtype in curves_dic:
name = curves_dic[crvtype]()
else:
raise NameError('Key "%s" not found' %(crvtype))
return name
So first question, if the method is never accessed from outside of the class or module, does it need to be an instance? And would a static or class method be appropriate in this case, since in this case I dont need seperate instances of this, it just does one thing, and never changes the size or shape, thats resized later.
Next, if i have a method in a class that just performs a simple calculation like below, would a static method be appropriate? Since its just taking input and outputting a single thing every time, is there any reason for it to be an instance?:
class Vectors(object):
"""Contains methods for various vector math functions
"""
@staticmethod
def pointLineDist(vec_a, vec_b, vec_c):
"""The distance between the line ab and the point c.
Args:
vec_a (float list): First vector to find distance of.
vec_b (float list): Second vector to find distance of.
vec_c (float list): Third vector to find distance of.
"""
ab = dt.Vector(vec_b) - dt.Vector(vec_a)
ac = dt.Vector(vec_c) - dt.Vector(vec_a)
length = dt.length(dt.cross(ab, ac)) / dt.length(ab)
return length
Lastly, I know a lot of people say in these cases, these shouldn't be in the class at all, or something like that, but this is the way I want it, I just want to know more in depth when to use these types of methods.