LearnFrenzy
Sign in Take a free test

Python Built-in Functions · Find the Error · Question 10

Error: getattr() without a default Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 10 Back to Exercise

The program below stops with an error. Which statement correctly describes the problem?

main.py
class Settings:
    theme = "dark"

for option in ["theme", "font_size"]:
    print(option, getattr(Settings, option))
  • A AttributeError: Settings has no font_size; pass a default, e.g. getattr(Settings, option, None) Correct
  • B It prints font_size None
  • C KeyError
  • D NameError
Correct Answer: Option A

"theme dark" prints first; the second lookup fails without a default.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links