Methods are functions inside a class. Different types of methods help organize code and control how data is accessed or changed.
When you want a function that works with an object's data.
When you need a function related to the class but not to any one object.
When you want a function that can change or use class-level 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):
# works without self or clspass
instance_method always has self to access object data.
class_method uses cls to access class data and is marked with @classmethod.
Examples
Instance method: works with a specific dog object.
Python
class Dog:
def bark(self):
print('Woof!')
Class method: works with the class itself, not one dog.
Python
class Dog:
species = 'Canine'
@classmethod
def get_species(cls):
return cls.species
Static method: does not use object or class data.
Python
class Dog:
@staticmethod
def info():
print('Dogs are friendly animals.')
Sample Program
This program shows three method types: instance method uses object data, class method uses class data, and static method works independently.
Python
class Car:
wheels = 4# class variabledef __init__(self, color):
self.color = color # instance variabledef show_color(self):
print(f'This car is {self.color}.') # instance method
@classmethod
def show_wheels(cls):
print(f'A car has {cls.wheels} wheels.') # class method
@staticmethod
def honk():
print('Beep beep!') # static method
my_car = Car('red')
my_car.show_color()
Car.show_wheels()
Car.honk()
OutputSuccess
Important Notes
Instance methods need an object to work.
Class methods can be called on the class or an object.
Static methods are like regular functions inside a class.
Summary
Instance methods use self to access object data.
Class methods use cls and @classmethod to access class data.
Static methods use @staticmethod and don't access object or class data.
Practice
(1/5)
1. Which method type in Python automatically receives the instance as the first argument named self?
easy
A. Instance method
B. Class method
C. Static method
D. Global function
Solution
Step 1: Understand method types
Instance methods receive the instance as the first argument, usually named self.
Step 2: Identify method with self
Class methods receive the class as cls, static methods receive no automatic first argument.
Final Answer:
Instance method -> Option A
Quick Check:
Method with self = Instance method [OK]
Hint: Look for self as first parameter for instance methods [OK]
Common Mistakes:
Confusing cls with self
Thinking static methods have self
Mixing global functions with methods
2. Which decorator is used to define a class method in Python?
easy
A. @classmethod
B. @staticmethod
C. @instance
D. @class
Solution
Step 1: Recall Python decorators for methods
Class methods use the @classmethod decorator to receive the class as the first argument.
Step 2: Identify correct decorator
@staticmethod is for static methods, @instance and @class are invalid decorators.
Final Answer:
@classmethod -> Option A
Quick Check:
Class method decorator = @classmethod [OK]
Hint: Class methods always use @classmethod decorator [OK]
Common Mistakes:
Using @staticmethod for class methods
Assuming @instance is a valid decorator
Confusing decorator names
3. What is the output of this code?
class MyClass:
@staticmethod
def greet():
return 'Hello'
print(MyClass.greet())
medium
A. AttributeError
B. TypeError
C. None
D. 'Hello'
Solution
Step 1: Understand static method behavior
Static methods do not require an instance or class argument and can be called directly on the class.
Step 2: Analyze the code output
The static method greet returns the string 'Hello', so print(MyClass.greet()) outputs 'Hello'.
Final Answer:
'Hello' -> Option D
Quick Check:
Static method call returns 'Hello' [OK]
Hint: Static methods can be called on class without arguments [OK]
Common Mistakes:
Expecting a TypeError for missing self
Confusing static with instance method call
Thinking static methods return None by default
4. Identify the error in this code:
class Example:
@classmethod
def show(cls):
print(cls.value)
Example.show()
medium
A. Missing @staticmethod decorator
B. Method should use 'self' instead of 'cls'
C. Missing class attribute 'value'
D. Syntax error in method definition
Solution
Step 1: Check class method usage
The method show is a class method and tries to print cls.value.
Step 2: Verify class attribute existence
The class Example does not define value, so accessing cls.value causes an AttributeError at runtime.
Final Answer:
Missing class attribute 'value' -> Option C
Quick Check:
Class attribute missing causes error [OK]
Hint: Class methods need class attributes to access via cls [OK]
Common Mistakes:
Confusing instance and class attributes
Thinking @staticmethod fixes attribute errors
Using 'self' in class methods incorrectly
5. You want a method that can be called on the class or instance but does NOT access instance or class data. Which method type should you use?
hard
A. Class method
B. Static method
C. Instance method
D. Property method
Solution
Step 1: Understand method access types
Instance methods access instance data via self, class methods access class data via cls.
Step 2: Identify method without data access
Static methods do not access instance or class data and can be called on both class and instance.
Final Answer:
Static method -> Option B
Quick Check:
No data access = Static method [OK]
Hint: No data access means use static method [OK]
Common Mistakes:
Choosing instance or class method incorrectly
Confusing property methods with static methods
Assuming static methods access class data
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.