@classmethod and @staticmethod
Define class-level and static methods with appropriate decorators.
@classmethod and @staticmethod is a free Python Academy lesson on CoddyKit. This is lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, and your progress syncs across the web and the CoddyKit app. The Python Academy course includes 4 lessons in total.
Introduction
Instance vs Class vs Static
class Demo:
def instance(self): return 'instance'
@classmethod
def class_m(cls): return f'class: {cls.__name__}'
@staticmethod
def static_m(): return 'static'
d = Demo()
print(d.instance())
print(Demo.class_m())
print(Demo.static_m())@classmethod as Factory
class Date:
def __init__(self, y,m,d): self.y,self.m,self.d=y,m,d
@classmethod
def today(cls):
from datetime import date
d = date.today()
return cls(d.year, d.month, d.day)
def __repr__(self): return f'{self.y}-{self.m}-{self.d}'
print(Date.today())@classmethod with Inheritance
class Animal:
@classmethod
def create(cls, name):
return cls(name)
def __init__(self, name): self.name = name
class Dog(Animal): pass
d = Dog.create('Rex')
print(type(d).__name__, d.name)@staticmethod for Utilities
class MathHelper:
@staticmethod
def clamp(value, lo, hi):
return max(lo, min(hi, value))
print(MathHelper.clamp(15, 0, 10))
print(MathHelper.clamp(-5, 0, 10))@staticmethod vs Module Function
class Validator:
@staticmethod
def is_email(s): return '@' in s and '.' in s
print(Validator.is_email('user@example.com'))
print(Validator.is_email('not-an-email'))Calling from Instance
class C:
@classmethod
def who(cls): return cls.__name__
@staticmethod
def pi(): return 3.14
c = C()
print(c.who())
print(c.pi())Class Method for Singletons
class Singleton:
_instance = None
@classmethod
def get(cls):
if cls._instance is None:
cls._instance = cls()
return cls._instance
a = Singleton.get()
b = Singleton.get()
print(a is b)Overriding Classmethods
class Base:
@classmethod
def info(cls): return f'{cls.__name__} info'
class Child(Base): pass
print(Base.info())
print(Child.info())Type Checking with cls
class Shape:
@classmethod
def make(cls):
return cls() # NOT Shape()
class Square(Shape): pass
print(type(Square.make()).__name__)Combining with @property
class Config:
_data = None
@classmethod
def load(cls):
if cls._data is None:
cls._data = {'debug': True}
return cls._data
print(Config.load())Quick Check
Recap
Keep Going
Learn Python with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 76
- Lessons
- 320
Frequently Asked Questions
Is the “@classmethod and @staticmethod” lesson free?
Yes — the full text of “@classmethod and @staticmethod” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO. The Python Academy course includes 4 lessons in total.
What will I learn in “@classmethod and @staticmethod”?
Define class-level and static methods with appropriate decorators. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Python Academy?
No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.
How long does the “@classmethod and @staticmethod” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Python Academy lesson?
Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Functions as First-Class Objects
- Writing Custom Decorators
- The @property Decorator
- @classmethod and @staticmethod