Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use a classmethod over an Instance method in python

Tags:

python

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 dont get why you would pick one method over the other

like image 787
Mantis Avatar asked Feb 28 '16 18:02

Mantis


People also ask

When should you use a Classmethod Python?

You can use class methods for any methods that are not bound to a specific instance but the class. In practice, you often use class methods for methods that create an instance of the class. When a method creates an instance of the class and returns it, the method is called a factory method.

What is the purpose of Classmethod and Staticmethod in Python?

The Static methods are used to do some utility tasks, and class methods are used for factory methods. The factory methods can return class objects for different use cases.

When should Staticmethod be used?

staticmethods can be used when the code that belongs to a class doesn't use the object itself at all. Python doesn't have to instantiate a bound method for each object we instantiate. Bound methods are objects too, and creating them has a cost. Having a static method avoids that.

Why do we need @staticmethod?

A static method has two main purposes: For utility or helper methods that don't require any object state. Since there is no need to access instance variables, having static methods eliminates the need for the caller to instantiate the object just to call the method.


1 Answers

In your second example, you've hard-coded the name and age into the class. If name and age are indeed properties of the class and not a specific instance of the class, than using a class method makes sense. However, if your class was something like Human of which there are many instances with different names and ages, then it wouldn't be possible to create a class method to access the unique names and ages of the specific instance. In that case, you would want to use an instance method.

In general:

  • If you want to access a property of a class as a whole, and not the property of a specific instance of that class, use a class method.
  • If you want to access/modify a property associated with a specific instance of the class, then you will want to use an instance method.
like image 117
SPKoder Avatar answered Sep 19 '22 00:09

SPKoder