Bird
Raised Fist0

Why might adding custom attributes directly to instances be discouraged in some cases?

hard🧠 Conceptual Q10 of Q15
Python - Custom Exceptions
Why might adding custom attributes directly to instances be discouraged in some cases?
AIt always raises an error when accessing attributes
BPython does not allow adding attributes dynamically
CIt can cause unexpected behavior if attributes are added inconsistently
DIt slows down the program significantly
Step-by-Step Solution
Solution:
  1. Step 1: Understand implications of dynamic attribute addition

    Adding attributes dynamically can make code harder to maintain and cause bugs if some instances have attributes others lack.
  2. Step 2: Evaluate options

    It can cause unexpected behavior if attributes are added inconsistently correctly states the risk of inconsistent attributes. Python does not allow adding attributes dynamically is false; Python allows dynamic attributes. It always raises an error when accessing attributes is false; errors occur only if attribute missing and accessed without check. It slows down the program significantly is false; performance impact is minimal.
  3. Final Answer:

    It can cause unexpected behavior if attributes are added inconsistently -> Option C
  4. Quick Check:

    Inconsistent dynamic attributes cause bugs [OK]
Quick Trick: Dynamic attributes can cause maintenance issues if inconsistent [OK]
Common Mistakes:
MISTAKES
  • Thinking Python forbids dynamic attributes
  • Assuming dynamic attributes always cause errors
  • Overestimating performance impact

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes