I have been lurking SO a long time and it happens that now it's my first time to ask a question since I couldn't find answer anywhere. I really hope that the title is not that confusing, I don't know how else to name it.
I am working on a Matrix class which I need for my college project (LUP decomposition, matrix inverse, solving linear equations...). Basic matrix operations, operator overloading, some helper methods, nothing too fancy.
As I was writing the code I came up with something that bothers me. I have a private method _makeMatrix. That method creates intended Matrix object. Then, I have classmethods like createFromList and createFromFile and similar, basically methods that allow a different way to create Matrix object. All of these methods call _makeMatrix to actually create Matrix object.
So, my question is, what is the difference between these two, except that in second case I can call _makeMatrix without creating object (which I obviously don't want to because _makeMatrix is intended to be private):
def _makeMatrix(r):
# some code that creates Matrix object m
return m
@classmethod
def createFromList(cls, matxList)
# code that makes matxList suitable for passing to _makeMatrix()
r = matxList
return Matrix._makeMatrix(r)
and
@classmethod
def _makeMatrix(cls, r):
# some code that creates Matrix object m
return m
@classmethod
def createFromList(cls, matxList)
# code that makes matxList suitable for passing to _makeMatrix()
r = matxList
return cls._makeMatrix(r)
What are the exact differences between these two and are there any benefits/drawbacks of using one or another approach?
c._makeMatrix(r). BTW, the correct name for this object per PEP-8 isclsselfshould be there. I've edited my code to address that.Matrix._makeMatrix(r)self, like your third example. I haven't realized that before, so what is going on here? Does Python implicitly treat that method as a static one, even though I didn't decorate it with@staticmethod? If I putselfin_makeMatrixmethod, I have to call it withcls._makeMatrix(cls, r), so I'm guessing there is also no difference between all these calls like in the previous examples? All this is a bit confusing, I have to admit.