Bird
Raised Fist0

Find the error in this constructor:

medium📝 Debug Q6 of Q15
Python - Constructors and Object Initialization
Find the error in this constructor:
class Book:
    def __init__(title, author):
        self.title = title
        self.author = author
AMissing self parameter in __init__
BWrong method name, should be init
CAttributes should not use self
DNo error, code is correct
Step-by-Step Solution
Solution:
  1. Step 1: Check constructor parameters

    Constructor must have self as first parameter to refer to the object.
  2. Step 2: Identify missing self

    Here, self is missing, causing error when assigning attributes.
  3. Final Answer:

    Missing self parameter in __init__ -> Option A
  4. Quick Check:

    self missing = constructor error [OK]
Quick Trick: Always include self as first constructor parameter [OK]
Common Mistakes:
MISTAKES
  • Omitting self parameter
  • Using wrong method name
  • Not using self to assign attributes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes