Bird
Raised Fist0

Identify the error in this code snippet:

medium📝 Debug Q6 of Q15
Python - Constructors and Object Initialization

Identify the error in this code snippet:

class Car:
    def __init__(self, model):
        model = model

c = Car('Tesla')
print(c.model)
AThe attribute is not assigned to self, so <code>c.model</code> does not exist.
BThe <code>__init__</code> method is missing the self parameter.
CThe class name should be lowercase.
DThe print statement syntax is incorrect.
Step-by-Step Solution
Solution:
  1. Step 1: Check attribute assignment

    The code assigns model = model, which only reassigns the local variable, not the instance attribute.
  2. Step 2: Understand consequence

    Since self.model is never set, c.model does not exist, causing an error.
  3. Final Answer:

    The attribute is not assigned to self, so c.model does not exist. -> Option A
  4. Quick Check:

    Instance attributes must be assigned to self [OK]
Quick Trick: Always assign attributes with self.attribute = value [OK]
Common Mistakes:
MISTAKES
  • Assigning local variable only
  • Missing self parameter
  • Incorrect print syntax

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes