Remember
A classmethod receives the class as its first argument. It can construct instances using the class through which it was called.
A classmethod receives the class as its first argument. It can construct instances using the class through which it was called.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
class Score:
def __init__(self, value):
self.value = value
@classmethod
def from_text(cls, text):
return cls(int(text))
score = Score.from_text('3')
print(score.value)Top-level scope
class ScoreObject 4 · ScoreScore({'value': 3})A classmethod receives the class as its first argument. It can construct instances using the class through which it was called.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
class Score:
def __init__(self, value):
self.value = value
@classmethod
def from_text(cls, text):
return cls(int(text))
score = Score.from_text('3')
print(score.value)
|
v
DISPLAYED RESULT
3
FINAL BINDINGS
Score --> object 2 (type)
score --> object 4 (Score)
object 2: class Score
object 4: Score({'value': 3})Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
class Score:
def __init__(self, value):
self.value = value
@classmethod
def from_text(cls, text):
return cls(int(text))
score = Score.from_text('3')
print(score.value)Top-level scope
class ScoreObject 4 · ScoreScore({'value': 3})The example has finished. Compare the final output with the values and references above.
3
What do you expect this program to print?
class Score:
def __init__(self, value):
self.value = value
@classmethod
def from_text(cls, text):
return cls(int(text))
score = Score.from_text('3')
print(score.value)classmethod() FunctionThe classmethod() function in Python is used to define a method that belongs to the class rather than to any specific instance of the class. This means you can access and modify class-level data.
classmethod(function)
function – The method you want to convert to a class method.class Person:
species = "Human"
def __init__(self, name):
self.name = name
@classmethod
def get_species(cls):
return cls.species
print(Person.get_species())
Human
@classmethod is a decorator that makes the method receive the class (cls) as the first argument.class Demo:
def normal_method():
pass
Demo.class_method = classmethod(Demo.normal_method)
Class methods are useful for creating factory methods—methods that return class instances using different input formats.
class Date:
def __init__(self, year, month, day):
self.year = year
self.month = month
self.day = day
@classmethod
def from_string(cls, date_str):
y, m, d = map(int, date_str.split('-'))
return cls(y, m, d)
d = Date.from_string("2025-05-14")
print(d.year, d.month, d.day)
2025 5 14
| Feature | classmethod() | staticmethod() |
|---|---|---|
| First Parameter | cls (the class) |
None |
| Access to Class Data | Yes | No |
| Typical Use | Factory methods, altering class-level state | Utility methods that don't access class or instance |
cls as the first argument in a class method.You may be asked to explain the difference between classmethod(), staticmethod(), and regular methods. Practice defining each one!
classmethod() binds a method to the class, not the instance.cls).Create a class Book with a class method from_string() that takes a string like "Title-Author" and returns a new Book object.
class Book:
def __init__(self, title, author):
self.title = title
self.author = author
@classmethod
def from_string(cls, book_str):
title, author = book_str.split('-')
return cls(title, author)
b = Book.from_string("Python101-John Doe")
print(b.title, b.author)
Python101 John Doe
Connect what you noticed in the example with the idea below.
A classmethod receives the class as its first argument. It can construct instances using the class through which it was called.
A classmethod always receives an instance as its first argument.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.