đ§ Python Class Methods & Attributes Guide¶
Beginner-friendly documentation about instance methods, class methods, static methods, attributes, and common object-oriented programming patterns in Python.
This document explains:
- instance methods
- class methods
- static methods
- instance attributes
- class attributes
- self
- cls
- common use cases
- practical examples
đ Table of Contents¶
- đ What are Methods?
- đ self
- đĻ Instance Attributes
- đī¸ Class Attributes
- âī¸ Instance Methods
- đˇī¸ Class Methods
- đ cls
- đ§Š Static Methods
- đ Method Comparison Table
- đī¸ Factory Methods
- đŽ Real 42 Examples
- â ī¸ Common Beginner Mistakes
- đ Best Practices
- đ Final Notes
đ What are Methods?¶
Methods are: - functions inside classes
Methods help objects: - perform actions - manipulate data - organize behavior
đ self¶
self refers to:
- the current object instance
Used to access: - object attributes - object methods
Example¶
class Player:
def __init__(self, name):
self.name = name
đĻ Instance Attributes¶
Instance attributes belong to: - individual objects
Each object stores its own values.
Example¶
class Drone:
def __init__(self, battery):
self.battery = battery
Creating Objects¶
drone1 = Drone(100)
drone2 = Drone(50)
Each object has: - different battery values
đī¸ Class Attributes¶
Class attributes belong to: - the class itself
Shared by ALL objects.
Example¶
class Drone:
max_speed = 10
Accessing Class Attributes¶
print(Drone.max_speed)
or:
drone = Drone()
print(drone.max_speed)
Why Class Attributes Are Useful¶
Useful for: - constants - shared settings - counters - global configuration
âī¸ Instance Methods¶
Instance methods work with: - object data
They receive:
- self
Example¶
class Player:
def jump(self):
print("Jump!")
Using the Method¶
player = Player()
player.jump()
When to Use Instance Methods¶
Use instance methods when: - behavior depends on object data
đˇī¸ Class Methods¶
Class methods work with: - the class itself
They receive:
- cls
instead of:
- self
Example¶
class Player:
total_players = 0
@classmethod
def show_total(cls):
print(cls.total_players)
đ cls¶
cls refers to:
- the class itself
Similar to how:
- self refers to the object
Using the Class Method¶
Player.show_total()
Why Class Methods Are Useful¶
Useful for: - shared class logic - object counters - factory methods - alternative constructors
đ§Š Static Methods¶
Static methods belong to the class but do NOT use:
- self
- cls
They behave like utility/helper functions.
Example¶
class MathUtils:
@staticmethod
def add(a, b):
return a + b
Using the Static Method¶
print(MathUtils.add(2, 3))
Output:
5
Why Static Methods Are Useful¶
Useful for: - utility functions - calculations - helper logic - formatting functions
đ Method Comparison Table¶
| Method Type | Uses self | Uses cls | Common Usage |
|---|---|---|---|
| Instance Method | â | â | Object behavior |
| Class Method | â | â | Shared class logic |
| Static Method | â | â | Utility/helper functions |
đī¸ Factory Methods¶
Factory methods are one of the most common uses of:
- @classmethod
Example¶
class Drone:
def __init__(self, battery):
self.battery = battery
@classmethod
def default_drone(cls):
return cls(100)
Using the Factory Method¶
drone = Drone.default_drone()
This creates: - a default object configuration
đŽ Real 42 Examples¶
Shared Configuration¶
class GameConfig:
TILE_SIZE = 64
Parser Helpers¶
class ConfigParser:
@staticmethod
def clean_line(line):
...
Object Counter¶
class Drone:
total_drones = 0
Factory Method¶
class Maze:
@classmethod
def default_maze(cls):
...
â ī¸ Common Beginner Mistakes¶
â Forgetting self¶
Wrong:
def jump():
Correct:
def jump(self):
â Using self in Static Methods¶
Wrong:
@staticmethod
def test():
print(self.name)
Static methods do not have:
- self
â Confusing Class and Instance Attributes¶
Wrong:
class Enemy:
hp = []
Mutable shared attributes may create bugs.
â Using Class Methods Like Instance Methods¶
Class methods work with: - class-level data
Not: - object-specific state
đ Best Practices¶
- Use instance methods for object behavior
- Use static methods for utilities
- Use class methods for shared class logic
- Keep classes focused
- Avoid giant classes
- Use meaningful method names
đ§ When to Use Each Method¶
Instance Method¶
Use when: - logic depends on object data
Class Method¶
Use when: - logic depends on class-wide data
Static Method¶
Use when: - no object/class state is required
đ Final Notes¶
Understanding methods and attributes is one of the most important parts of Python OOP.
These concepts help: - organize projects - improve readability - structure large applications - reduce duplicated logic
They become especially useful in: - MLX projects - parsers - APIs - games - collaborative repositories