Methods help organize code inside classes. Different method types serve different jobs to keep code clear and easy to use.
Use instance methods when you want to work with data unique to each object.
Use class methods when you need to change or access data shared by all objects of a class.
Use static methods when the task relates to the class but does not need any object or class data.
Syntax
Python
class ClassName:
def instance_method(self, args):
# works with object datapass
@classmethod
def class_method(cls, args):
# works with class datapass
@staticmethod
def static_method(args):
# independent function inside classpass
self refers to the object calling the method.
cls refers to the class itself, not an object.
Examples
Instance method bark uses object data name.
Python
class Dog:
def __init__(self, name):
self.name = name
def bark(self):
print(f"{self.name} says Woof!")
Class method get_species accesses shared class data species.
Python
class Dog:
species = "Canis familiaris"
@classmethod
def get_species(cls):
return cls.species
Static method is_dog checks something related but does not use object or class data.
Python
class Dog:
@staticmethod
def is_dog(animal):
return animal.lower() == "dog"
Sample Program
This program shows all three method types working together in a class.
Python
class Car:
wheels = 4# class attributedef __init__(self, color):
self.color = color # instance attributedef paint(self, new_color):
self.color = new_color # instance method changes object data
@classmethod
def number_of_wheels(cls):
return cls.wheels # class method returns shared data
@staticmethod
def honk():
print("Beep beep!") # static method unrelated to object or class data
car1 = Car("red")
print(car1.color) # red
car1.paint("blue")
print(car1.color) # blueprint(Car.number_of_wheels()) # 4
Car.honk()
OutputSuccess
Important Notes
Instance methods always need self to access object data.
Class methods use cls to access or change class-wide data.
Static methods are like normal functions but kept inside the class for organization.
Summary
Instance methods work with data unique to each object.
Class methods work with data shared by all objects of the class.
Static methods perform tasks related to the class but don't use object or class data.
Practice
(1/5)
1. Which method type in Python is used to access or modify data unique to each object instance?
easy
A. Global function
B. Instance method
C. Static method
D. Class method
Solution
Step 1: Understand instance methods
Instance methods receive the object itself as the first argument and can access or modify instance-specific data.
Step 2: Compare with other methods
Class methods work with class-level data, static methods don't access instance or class data, and global functions are outside the class.
Final Answer:
Instance method -> Option B
Quick Check:
Instance method = unique object data [OK]
Hint: Instance methods use 'self' to access object data [OK]
Common Mistakes:
Confusing class methods with instance methods
Thinking static methods access instance data
Assuming global functions are methods
2. Which of the following is the correct way to define a class method in Python?
easy
A. def method():
B. def method(self):
C. @staticmethod\ndef method():
D. @classmethod\ndef method(cls):
Solution
Step 1: Recall class method syntax
Class methods use the @classmethod decorator and receive the class as the first argument, usually named 'cls'.
Step 2: Check options
@classmethod\ndef method(cls): matches this syntax exactly; others define instance or static methods or lack decorators.
Final Answer:
@classmethod\ndef method(cls): -> Option D
Quick Check:
Class method = @classmethod + cls parameter [OK]
Hint: Class methods use @classmethod and 'cls' parameter [OK]
Each time an Example object is created, the class variable 'count' increases by 1. Two objects are created, so count becomes 2.
Step 2: Understand the class method output
The class method 'get_count' returns the current value of 'count', which is 2 after creating two objects.
Final Answer:
2 -> Option A
Quick Check:
Class variable incremented twice = 2 [OK]
Hint: Class methods access shared data like 'count' [OK]
Common Mistakes:
Thinking count resets per instance
Confusing instance and class variables
Expecting an error due to method call
4. Identify the error in this code snippet:
class Calculator:
@staticmethod
def add(x, y):
return x + y
@classmethod
def multiply(cls, x, y):
return x * y
print(Calculator.add(2, 3))
print(Calculator.multiply(2, 3))
medium
A. No error, code runs correctly
B. Class method 'multiply' missing 'self' parameter
C. Static method 'add' missing 'self' parameter
D. Class method 'multiply' missing 'cls' parameter
Solution
Step 1: Check static method definition
Static methods do not require 'self' or 'cls'; 'add' is correctly defined with two parameters.
Step 2: Check class method definition
Class method 'multiply' correctly has 'cls' as the first parameter and two others; usage is correct.
Final Answer:
No error, code runs correctly -> Option A
Quick Check:
Static and class methods correctly defined [OK]
Hint: Static methods no 'self'; class methods need 'cls' first [OK]
Common Mistakes:
Expecting 'self' in static methods
Confusing 'self' and 'cls' in class methods
Assuming method calls require instance
5. You want to create a method in a class that logs a message but does not need access to instance or class data. Which method type should you use and why?
hard
A. Instance method, because it can access instance data
B. Class method, because it can access class data
C. Static method, because it does not require instance or class data
D. Global function, because it is outside the class
Solution
Step 1: Identify method requirements
The method only logs a message and does not need to access instance or class data.
Step 2: Choose appropriate method type
Static methods are designed for tasks related to the class but do not use instance or class data, making them ideal here.
Final Answer:
Static method, because it does not require instance or class data -> Option C
Quick Check:
Logging without data access = static method [OK]
Hint: Use static methods for utility tasks without data access [OK]
Common Mistakes:
Using instance or class methods unnecessarily
Confusing static methods with global functions
Thinking logging requires instance data
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.