Bird
Raised Fist0
Pythonprogramming~10 mins

Difference between method types in Python - Visual Side-by-Side Comparison

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Difference between method types
Call instance.method(obj)
Uses self, accesses instance data
Call class.method(cls)
Uses cls, accesses class data
Call static.method()
No self or cls, independent function
Shows how instance, class, and static methods are called and what data they access.
Execution Sample
Python
class Example:
    def instance_method(self):
        return f"Instance method called, self={self}"

    @classmethod
    def class_method(cls):
        return f"Class method called, cls={cls}"

    @staticmethod
    def static_method():
        return "Static method called"
Defines a class with three method types to show their differences.
Execution Table
StepCallMethod TypeParameter PassedOutput
1obj.instance_method()Instance methodself = objInstance method called, self=<Example object>
2Example.class_method()Class methodcls = ExampleClass method called, cls=<class 'Example'>
3Example.static_method()Static methodNo parametersStatic method called
4obj.class_method()Class methodcls = ExampleClass method called, cls=<class 'Example'>
5obj.static_method()Static methodNo parametersStatic method called
6Example.instance_method()ErrorMissing selfTypeError: missing 1 required positional argument: 'self'
💡 Step 6 raises error because instance_method requires an instance (self) but called on class.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6
selfN/A<Example object><Example object><Example object><Example object><Example object>Error
clsN/AN/A<class 'Example'><class 'Example'><class 'Example'><class 'Example'>N/A
Key Moments - 3 Insights
Why does calling instance_method on the class (Example.instance_method()) cause an error?
Because instance_method expects an instance as the first argument (self), but calling it on the class does not provide one. See execution_table step 6.
How can class_method be called from an instance?
Class methods receive the class as the first argument (cls), so calling obj.class_method() works and passes the class automatically. See execution_table step 4.
Do static methods receive any automatic arguments?
No, static methods do not receive self or cls automatically. They behave like regular functions inside the class namespace. See execution_table steps 3 and 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what parameter is passed to class_method when called as obj.class_method() at step 4?
Acls (the class)
Bself (the instance)
CNo parameters
DBoth self and cls
💡 Hint
Check the 'Parameter Passed' column in execution_table row for step 4.
At which step does calling a method cause an error due to missing parameters?
AStep 2
BStep 4
CStep 6
DStep 3
💡 Hint
Look for 'Error' in the 'Method Type' column in execution_table.
If static_method needed to access instance data, what would happen?
AIt would work normally
BIt would raise an error because no self is passed
CIt would receive cls automatically
DIt would convert to a class method
💡 Hint
Static methods do not get self or cls automatically, see key_moments about static methods.
Concept Snapshot
Instance method: first parameter is self (instance), accesses instance data.
Class method: decorated with @classmethod, first parameter is cls (class), accesses class data.
Static method: decorated with @staticmethod, no automatic parameters, behaves like a normal function.
Instance methods require an instance to be called.
Class and static methods can be called on class or instance.
Calling instance method on class without instance causes error.
Full Transcript
This lesson shows the difference between instance, class, and static methods in Python. Instance methods receive the instance as the first parameter called self and can access instance data. Class methods are decorated with @classmethod and receive the class as the first parameter called cls, allowing access to class data. Static methods are decorated with @staticmethod and do not receive any automatic parameters; they behave like regular functions inside the class. The execution table shows calls to each method type and what parameters are passed. Calling an instance method on the class without an instance causes an error because self is missing. Class methods can be called from both the class and instances, receiving the class automatically. Static methods can also be called from both but do not receive self or cls. The variable tracker shows how self and cls change during calls. Key moments clarify common confusions about method calls and parameters. The visual quiz tests understanding of parameters passed and errors raised during calls.

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

  1. Step 1: Understand method types

    Instance methods receive the instance as the first argument, usually named self.
  2. Step 2: Identify method with self

    Class methods receive the class as cls, static methods receive no automatic first argument.
  3. Final Answer:

    Instance method -> Option A
  4. 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

  1. Step 1: Recall Python decorators for methods

    Class methods use the @classmethod decorator to receive the class as the first argument.
  2. Step 2: Identify correct decorator

    @staticmethod is for static methods, @instance and @class are invalid decorators.
  3. Final Answer:

    @classmethod -> Option A
  4. 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

  1. Step 1: Understand static method behavior

    Static methods do not require an instance or class argument and can be called directly on the class.
  2. Step 2: Analyze the code output

    The static method greet returns the string 'Hello', so print(MyClass.greet()) outputs 'Hello'.
  3. Final Answer:

    'Hello' -> Option D
  4. 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

  1. Step 1: Check class method usage

    The method show is a class method and tries to print cls.value.
  2. Step 2: Verify class attribute existence

    The class Example does not define value, so accessing cls.value causes an AttributeError at runtime.
  3. Final Answer:

    Missing class attribute 'value' -> Option C
  4. 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

  1. Step 1: Understand method access types

    Instance methods access instance data via self, class methods access class data via cls.
  2. 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.
  3. Final Answer:

    Static method -> Option B
  4. 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