While integrating a Django app I have not used before, I found two different ways to define functions inside the class. The author seems to use them both distinctively and intentionally. The first one is the one that I myself use a lot:
class Dummy(object):
def some_function(self, *args, **kwargs):
# do something here
# self is the class instance
The other one is the one I never use, mostly because I do not understand when and what to use it for:
class Dummy(object):
@classmethod
def some_function(cls, *args, **kwargs):
# do something here
# cls refers to what?
The classmethod
decorator in the python documentation says:
A class method receives the class as the implicit first argument, just like an instance method receives the instance.
So I guess cls
refers to Dummy
itself (the class
, not the instance). I do not exactly understand why this exists, because I could always do this:
type(self).do_something_with_the_class
Is this just for the sake of clarity, or did I miss the most important part: spooky and fascinating things that couldn't be done without it?