- ✕This summary was generated using AI based on multiple online sources. To view the original source information, use the "Learn more" links.
A class method in Python is a method that is bound to the class rather than its instances. It receives the class itself as the first argument, conventionally named cls, instead of self. This allows it to access and modify class-level attributes that are shared across all instances.
You can define a class method using either the @classmethod decorator or the built-in classmethod() function. Class methods are often used for factory methods, which create instances in alternative ways, and for operations that affect the class as a whole rather than individual objects.
Example using @classmethod decorator:
from datetime import dateclass Person:def __init__(self, name, age):self.name = nameself.age = age@classmethoddef from_birth_year(cls, name, birth_year):return cls(name, date.today().year - birth_year)def display(self):print(f"{self.name}'s age is: {self.age}")# Creating instance using constructorp1 = Person("Alice", 30)# Creating instance using class method (factory method)p2 = Person.from_birth_year("Bob", 1990)p1.display()p2.display()Copied!✕Copy classmethod () in Python - GeeksforGeeks
Jul 11, 2025 · In this example, we are going to see how to create a class method in Python. For this, we created a class named "Geeks" with a member variable "course" and created a function named …
See results only from geeksforgeeks.orgSign In
In this example, we are going to see how to create a class method in Python. For this, we created a class named "Geeks" with a member variable "course" and cre…
Python Class Methods - W3Schools
Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, Python, PHP, Bootstrap, Java, XML and more.
classmethod() | Python’s Built-in Functions – Real Python
A common real-world application of @classmethod is to create factory methods that return pre-configured instances of a class. For example, a Pizza class might …
Python Class Method Explained With Examples - PYnative
Aug 28, 2021 · Class methods are methods that are called on the class itself, not on a specific object instance. Therefore, it belongs to a class level, and all class …
9. Classes — Python 3.14.3 documentation
Mar 25, 2026 · Creating a new class creates a new type of object, allowing new instances of that type to be made. Each class instance can have attributes …
Searches you might like
An Essential Guide to Python Class Methods and When to Use Them
In this tutorial, you'll learn about Python class methods and when to use them appropriately.
Python classmethod () - Programiz
In this tutorial, we will learn about the Python classmethod () function with the help of examples.
Python Classmethod - pythonbasics.org
A class method is a method that's shared among all objects. To call a class method, put the class as the first argument. Class methods can be can be called from instances and from the class itself. All of …
Class Methods in Python
Learn about class methods in Python, a type of method that operates on the class itself rather than individual instances. This guide explains how to d
Python classmethod Function - Complete Guide - ZetCode
Apr 11, 2025 · Complete guide to Python's classmethod decorator covering definition, usage, and practical examples of class methods.