GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||
GeezerVison is showing www.geeksforgeeks.org — Class method vs Static method in Python - GeeksforGeeks Class method vs Static method in PythonLast Updated : 20 Mar, 2026 Class methods and static methods are special types of methods in Python that are bound to a class rather than its instances. They are used when behavior logically belongs to the class but does not always require access to instance-specific data. Class MethodA class method is a method that receives the class itself as the first argument, conventionally named cls. It can access and modify class-level data and is often used to define factory methods. A factory method is a method that creates and returns an object of the class. It acts as an alternative constructor, allowing objects to be created in different ways. Below is the syntax of class method:
Parameters:
Example: This example uses a class method as a factory method to create an object using a birth year instead of directly passing age.
Output Jake 26 Explanation: @classmethod allows from_birth_year() to receive cls and cls(name, age) creates a new object of the class. Static MethodA static method is a method that does not receive self or cls automatically. It behaves like a normal function but is placed inside a class because it logically belongs there. It cannot access or modify class or instance data directly. A utility function is a helper function that performs a task related to the class but does not depend on class-level or instance-level data. Below is the syntax of static method:
Parameters:
Example: This example defines a utility function inside a class to check whether a person is an adult.
Output True False Explanation: @staticmethod defines is_adult() as a utility function. It does not access self or cls, and works only with the provided parameter age. Class method vs Static MethodThe key difference between a class method and a static method is whether the method needs access to the class itself.
Comment Explore | |||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |