Methods are functions inside a class. Different types of methods help organize code and control how data is accessed or changed.
Difference between method types in Python
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Python
class ClassName: def instance_method(self, args): # works with object data pass @classmethod def class_method(cls, args): # works with class data pass @staticmethod def static_method(args): # works without self or cls pass
instance_method always has self to access object data.
class_method uses cls to access class data and is marked with @classmethod.
Examples
Python
class Dog: def bark(self): print('Woof!')
Python
class Dog: species = 'Canine' @classmethod def get_species(cls): return cls.species
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 variable def __init__(self, color): self.color = color # instance variable def 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()
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. Which method type in Python automatically receives the instance as the first argument named
self?easy
Solution
Step 1: Understand method types
Instance methods receive the instance as the first argument, usually namedself.Step 2: Identify method with
Class methods receive the class asselfcls, static methods receive no automatic first argument.Final Answer:
Instance method -> Option AQuick Check:
Method withself= Instance method [OK]
Hint: Look for
self as first parameter for instance methods [OK]Common Mistakes:
- Confusing
clswithself - Thinking static methods have
self - Mixing global functions with methods
2. Which decorator is used to define a class method in Python?
easy
Solution
Step 1: Recall Python decorators for methods
Class methods use the@classmethoddecorator to receive the class as the first argument.Step 2: Identify correct decorator
@staticmethodis for static methods,@instanceand@classare invalid decorators.Final Answer:
@classmethod -> Option AQuick 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
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 methodgreetreturns the string 'Hello', soprint(MyClass.greet())outputs 'Hello'.Final Answer:
'Hello' -> Option DQuick 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
Solution
Step 1: Check class method usage
The methodshowis a class method and tries to printcls.value.Step 2: Verify class attribute existence
The classExampledoes not definevalue, so accessingcls.valuecauses an AttributeError at runtime.Final Answer:
Missing class attribute 'value' -> Option CQuick 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
Solution
Step 1: Understand method access types
Instance methods access instance data viaself, class methods access class data viacls.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 BQuick 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
