Skip to content

Question 26

What is the output of the following code?

1
2
3
4
5
6
7
class Quote():
    def __init__(self, name, quote):
        self.name = name
        self.say = name + ': ' + quote

teacher = Quote('Alan', 'Code, pray, love')
print(teacher.say)
Hint

The Quote class contains 2 class members (or attributes/properties; will use these terms interchangably, refer here for more). Form their respective values with the parameters passed into the constructor.

Solution

teacher is initialized as an object of class Quote with the following class members:

  • self.name: name = 'Alan'
  • self.say: name + ': ' + quote = 'Alan: Code, pray, love'

When any of these attributes are called (e.g., teacher.say), they will now return their respective values accordingly.

Answer
Alan: Code, pray, love