I have been trying to get my head around classmethods for a while now. I know how they work but I don't understand why use them or not use them.
For example:
I know i can use an instance method like this:
class MyClass():
def __init__(self):
self.name = 'Chris'
self.age = 27
def who_are_you(self):
print('Hello {}, you are {} years old'.format(self.name, self.age))
c = MyClass()
c.who_are_you()
I also know that by using the classmethod I can call the who_are_you() without creating an instance of my class:
class MyClass():
name = 'Chris'
age = 27
@classmethod
def who_are_you(cls):
print('Hello {}, you are {} years old'.format(cls.name, cls.age))
MyClass.who_are_you()
I don't get why you would pick one method over the other