LearnFrenzy
Sign in Take a free test

OOP in Python · Find the Error · Question 2

Error: creating an object without a required argument Answer, explanation & discussion

  • Find the error
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 2 Back to Exercise

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

main.py
class Product:
    def __init__(self, name, price):
        self.name = name
        self.price = price

p = Product("Pen")
print(p.name)
  • A It prints Pen
  • B AttributeError: price
  • C TypeError: __init__ requires price but only name was given; pass it or give price a default Correct
  • D NameError
Correct Answer: Option C

Arguments to the class call go to __init__ and follow the same rules as a function call.

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