WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Python · Intermediate · question 31 of 100

What are the differences between a class method, a static method, and an instance method in Python? Provide examples.?

📕 Buy this interview preparation book: 100 Python questions & answers — PDF + EPUB for $5

In Python, there are three types of methods that can be defined in a class: instance methods, class methods, and static methods. Each type of method has a different purpose and behavior. Here are the differences between them:

Instance methods: Instance methods are the most common type of method in Python classes. They are defined inside the class and are called on an instance of the class. Instance methods take the instance itself as the first argument, conventionally named self. Instance methods can access and modify the instance’s attributes and properties. Here is an example:

    class MyClass:
        def __init__(self, name):
        self.name = name
    
    def say_hello(self):
        print(f"Hello, my name is {self.name}")
    
    my_obj = MyClass("John")
    my_obj.say_hello()  # Output: "Hello, my name is John"

Class methods: Class methods are methods that are bound to the class and not the instance of the class. They take the class itself as the first argument, conventionally named cls. Class methods can be used to create alternate constructors or perform class-level operations that don’t require access to instance-level data. Class methods are defined using the @classmethod decorator. Here is an example:

    class MyClass:
        count = 0
    
        def __init__(self, name):
            self.name = name
            MyClass.count += 1
    
        @classmethod
        def get_count(cls):
            return cls.count
    
    my_obj1 = MyClass("John")
    my_obj2 = MyClass("Jane")
    print(MyClass.get_count())  # Output: 2

In this example, the get_count method is defined as a class method using the @classmethod decorator. It accesses the count attribute of the class, which is incremented every time a new instance of the class is created.

Static methods: Static methods are methods that don’t require an instance or the class itself as the first argument. They are defined inside the class and can be called on the class or an instance of the class. Static methods are often used for utility functions that don’t depend on instance or class-level data. Static methods are defined using the @staticmethod decorator. Here is an example:

    class MyClass:
        @staticmethod
        def add_numbers(x, y):
            return x + y
    
    result = MyClass.add_numbers(2, 3)
    print(result)  # Output: 5

In this example, the add_numbers method is defined as a static method using the @staticmethod decorator. It takes two arguments and returns their sum. The method can be called on the class itself, without creating an instance of the class.

In summary, instance methods, class methods, and static methods are three types of methods that can be defined in a Python class. Instance methods are called on an instance of the class, class methods are bound to the class and take the class itself as the first argument, and static methods don’t require an instance or the class itself as the first argument.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Python interview — then scores it.
📞 Practice Python — free 15 min
📕 Buy this interview preparation book: 100 Python questions & answers — PDF + EPUB for $5

All 100 Python questions · All topics