Python

Class Methods & Static Methods

Master different method types. Learn @classmethod, @staticmethod, and factory patterns.

By TechCoder TeamLast updated: 2026-06-02
In a Nutshell

Master different method types. Learn @classmethod, @staticmethod, and factory patterns. This hands-on tutorial focuses on practical implementation of class methods & static methods concepts.

Class Methods & Static Methods

Python has three types of methods: instance methods, class methods, and static methods. Each serves a different purpose.

Method Types Comparison

TypeDecoratorFirst ParameterAccess ToUse Case
InstanceNoneselfInstance & classRegular methods
Class@classmethodclsClass onlyFactory methods, alternatives
Static@staticmethodNoneNothingUtility functions

Instance Methods (Review)

These are the regular methods you've been using. They receive self and can access instance attributes.

PYTHON PLAYGROUND
⏳ Loading editor…

Class Methods: @classmethod 🏭

Class methods receive the class itself as the first parameter (cls), not an instance.

Use cases:

  • Factory methods (alternative constructors)
  • Modify class state
  • Access class variables
PYTHON PLAYGROUND
⏳ Loading editor…

Static Methods: @staticmethod πŸ”§

Static methods don't receive self or cls. They're just regular functions that belong to the class namespace.

Use cases:

  • Utility functions related to the class
  • Functions that don't need instance or class data
PYTHON PLAYGROUND
⏳ Loading editor…

Factory Pattern with Class Methods 🏭

Class methods are perfect for creating alternative constructors.

PYTHON PLAYGROUND
⏳ Loading editor…

When to Use Each Type πŸ€”

PYTHON PLAYGROUND
⏳ Loading editor…

Coding Challenge: Employee Management System πŸ‘”

Create an Employee class with all three method types.

PYTHON PLAYGROUND
⏳ Loading editor…

AI Mentor

Confused about "Python class methods static methods decorators"? Ask our AI mentor for a simplified explanation.

Quiz

Quiz

Question 1 of 5

What parameter does a class method receive?

self
cls
class
None

Key Takeaways

βœ… Instance methods use self and access instance data.
βœ… Class methods use @classmethod and cls for class-level operations.
βœ… Static methods use @staticmethod for utility functions.
βœ… Factory pattern uses class methods for alternative constructors.
βœ… Choose based on what data you need to access.

What's Next?

In the next lesson, we'll do a Decorators Deep Dive - creating custom decorators and understanding how they work.

Keep coding! πŸš€