Python Interview Questions

32 Questions
Python

Python

Web DevelopmentFrontendBackendData Science

Question 19

What are class methods and static methods?

Answer:

In Python, class methods and static methods are two types of methods that can be defined within a class, in addition to instance methods. Both types of methods are used for different purposes and have distinct characteristics. Here’s an explanation of each:

Class Methods

Class methods are methods that are bound to the class itself rather than to its instances. They can access and modify class state that applies across all instances of the class. Class methods are defined using the @classmethod decorator and take cls as their first parameter, which refers to the class itself.

Key Points:

  • Bound to the class, not the instance.
  • Can modify class-level attributes.
  • Defined using the @classmethod decorator.
  • First parameter is cls, which represents the class.

Example:

class MyClass:
    class_variable = 0

    def __init__(self):
        MyClass.class_variable += 1

    @classmethod
    def get_class_variable(cls):
        return cls.class_variable

# Creating instances
obj1 = MyClass()
obj2 = MyClass()

# Calling the class method
print(MyClass.get_class_variable())  # Output: 2
print(obj1.get_class_variable())     # Output: 2

In this example:

  • get_class_variable is a class method that returns the value of the class variable class_variable.
  • The class method is called using both the class name and an instance, but it accesses the class-level state.

Static Methods

Static methods are methods that do not depend on class or instance-specific data. They are utility functions that perform a task in isolation. Static methods are defined using the @staticmethod decorator and do not take self or cls as their first parameter.

Key Points:

  • Not bound to the class or instance.
  • Cannot modify class or instance state.
  • Defined using the @staticmethod decorator.
  • Do not take self or cls as a parameter.

Example:

class MyClass:
    @staticmethod
    def static_method(arg1, arg2):
        return arg1 + arg2

# Calling the static method
print(MyClass.static_method(5, 10))  # Output: 15

# Creating an instance and calling the static method
obj = MyClass()
print(obj.static_method(3, 7))       # Output: 10

In this example:

  • static_method is a static method that takes two arguments and returns their sum.
  • The static method is called using both the class name and an instance, but it operates independently of class or instance-specific data.

Summary of Differences

Feature Instance Method Class Method Static Method
Bound to Instance Class Neither
First Parameter self (instance reference) cls (class reference) None
Access to Instance attributes and methods Class attributes and methods No access to class or instance attributes/methods
Use Case Access and modify instance data Access and modify class-level data Utility functions, independent of class/instance data

Use Cases

Class Methods:

  • Factory methods that create instances in different ways.
  • Methods that need to access or modify class-level state.
  • Methods that are logically associated with the class but not with instances.

Static Methods:

  • Utility functions that perform operations related to the class but do not need to access class or instance data.
  • Functions that could logically be standalone but are grouped within the class for organizational purposes.

By using class methods and static methods appropriately, you can create more organized and logical code, separating instance-level behavior from class-level and independent utility functions.

Recent job openings