Bird
Raised Fist0

Identify the mistake in this class definition:

medium📝 Debug Q6 of Q15
Python - Constructors and Object Initialization
Identify the mistake in this class definition:
class Movie:
    def __init__(self, title, director):
        title = title
        director = director

m = Movie('Inception', 'Nolan')
AThe parameters should be assigned outside the __init__ method
BThe __init__ method is missing the self parameter
CThe class name should be lowercase
DThe attributes are not assigned to self, so they won't be stored in the object
Step-by-Step Solution
Solution:
  1. Step 1: Check parameter usage

    The parameters title and director are passed correctly with self.
  2. Step 2: Attribute assignment

    Inside __init__, attributes must be assigned to self (e.g., self.title = title) to store them in the object.
  3. Step 3: Identify error

    Here, title = title just reassigns the local variable and does not set the object's attribute.
  4. Final Answer:

    The attributes are not assigned to self, so they won't be stored in the object -> Option D
  5. Quick Check:

    Attributes must be assigned to self [OK]
Quick Trick: Assign attributes to self to store them [OK]
Common Mistakes:
MISTAKES
  • Assigning parameters to themselves instead of self attributes
  • Forgetting to use self in attribute assignment
  • Confusing local variables with instance variables

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes