Bird
0
0

Find the error in this code:

medium📝 Debug Q14 of 15
Python - Classes and Object Lifecycle
Find the error in this code:
class Car:
    def __init__(self, model):
        model = model

car = Car("Tesla")
print(car.model)
AAttributeError because model is not set as instance attribute
BSyntaxError due to missing self
CTypeError because __init__ has wrong parameters
DNo error, prints Tesla
Step-by-Step Solution
Solution:
  1. Step 1: Check attribute assignment in __init__

    The code assigns model = model, which only assigns local variable, not instance attribute.
  2. Step 2: Accessing car.model causes error

    Since self.model is never set, car.model does not exist, causing AttributeError.
  3. Final Answer:

    AttributeError because model is not set as instance attribute -> Option A
  4. Quick Check:

    Use self.model = model to set instance attribute [OK]
Quick Trick: Always use self.attribute = value to set instance attributes [OK]
Common Mistakes:
  • Forgetting self. when assigning attributes
  • Assuming local variable sets instance attribute
  • Expecting attribute to exist without self

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes