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:
Step 1: Understand encapsulation concept
Encapsulation means hiding internal details. Directly changing attributes can bypass validation or logic.
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.
Final Answer:
It can break encapsulation and cause unexpected side effects. -> Option B
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
Master "Classes and Object Lifecycle" in Python
9 interactive learning modes - each teaches the same concept differently