🐍 Python Intermediate

What are Python's class and static methods?

Why Interviewers Ask This

This tests whether you can apply Python knowledge to real-world scenarios. Interviewers are looking for clarity of thought and evidence that you've encountered this in production code.

Answer

Python classes have three types of methods. Instance methods (default): receive self (the instance) as first argument — can access and modify instance and class state. Class methods decorated with @classmethod: receive cls (the class) as first argument — can access and modify class state but not instance state. Used as alternative constructors: @classmethod def from_string(cls, s): return cls(*s.split(",")). Static methods decorated with @staticmethod: receive no implicit first argument — they are just regular functions namespaced inside the class. They cannot access or modify class or instance state. Use for utility functions related to the class but not dependent on it. Example: @staticmethod def validate(value): return isinstance(value, int) and value > 0.

Pro Tip

This topic has Python-specific nuances that differ from general programming. Highlighting those nuances in your answer shows expertise rather than generic knowledge.