Bird
Raised Fist0

Why might directly modifying an attribute of an object be risky in some cases?

hard🧠 Conceptual Q10 of Q15
Python - Classes and Object Lifecycle
Why might directly modifying an attribute of an object be risky in some cases?
APython does not allow attribute modification after creation.
BIt can break encapsulation and cause unexpected side effects.
CAttributes are always read-only by default.
DModifying attributes requires special syntax not shown here.
Step-by-Step Solution
Solution:
  1. Step 1: Understand encapsulation concept

    Encapsulation means hiding internal details. Directly changing attributes can bypass validation or logic.
  2. Step 2: Evaluate options

    It can break encapsulation and cause unexpected side effects. correctly states the risk of breaking encapsulation. Options B and C are false; Python allows attribute changes and they are not read-only by default. Modifying attributes requires special syntax not shown here. is incorrect; normal syntax suffices.
  3. Final Answer:

    It can break encapsulation and cause unexpected side effects. -> Option B
  4. Quick Check:

    Direct attribute change risks encapsulation = It can break encapsulation and cause unexpected side effects. [OK]
Quick Trick: Use getters/setters to protect attribute changes [OK]
Common Mistakes:
MISTAKES
  • Thinking attributes are immutable by default
  • Believing special syntax is always needed
  • Ignoring design principles like encapsulation

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes