Bird
Raised Fist0
Pythonprogramming~10 mins

Difference between method types in Python - Interactive Practice

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
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define an instance method that prints the name.

Python
class Person:
    def __init__(self, name):
        self.name = name
    def show_name(self):
        print(self.[1])
Drag options to blanks, or click blank then click option'
Aself
Bname
CPerson
Dcls
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'self' instead of 'name' inside print statement.
Using 'cls' which is for class methods.
Using 'Person' which is the class name, not an instance variable.
2fill in blank
medium

Complete the code to define a class method that prints the class name.

Python
class Person:
    @classmethod
    def show_class(cls):
        print(cls.[1])
Drag options to blanks, or click blank then click option'
Aname
B__name__
C__class__
D__qualname__
Attempts:
3 left
💡 Hint
Common Mistakes
Using instance variable names like 'name'.
Using __class__ which is not an attribute of cls.
Using __name__ which is a module attribute.
3fill in blank
hard

Fix the error in the static method that returns a greeting string.

Python
class Person:
    @staticmethod
    def greet():
        return 'Hello, ' + [1]
Drag options to blanks, or click blank then click option'
A'World'
Bcls.name
Cself.name
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to access instance or class variables inside static method.
Using undefined variable 'name'.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps words to their lengths only if length is greater than 3.

Python
words = ['apple', 'bat', 'carrot', 'dog']
lengths = {word: [1] for word in words if [2]
Drag options to blanks, or click blank then click option'
Alen(word)
Blen(word) > 3
Cword > 3
Dword.length
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'word > 3' which compares string to number.
Using 'word.length' which is not valid in Python.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase words to their lengths only if length is less than 6.

Python
words = ['apple', 'bat', 'carrot', 'dog']
result = [1]: [2] for word in words if [3]
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
Clen(word) < 6
Dword.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using word.lower() instead of uppercase.
Using wrong length condition like greater than 6.

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