LearnFrenzy
Sign in Take a free test

OOP in Python · Find the Error · Question 4

Error: an attribute used without self. Answer, explanation & discussion

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

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

main.py
class Circle:
    def __init__(self, radius):
        self.radius = radius

    def area(self):
        return 3.14 * radius ** 2

print(Circle(2).area())
  • A NameError: inside a method, attributes must be accessed through self – write self.radius Correct
  • B It prints 12.56
  • C AttributeError
  • D TypeError
Correct Answer: Option A

Unlike Java or C++, Python never looks up attributes implicitly.

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