Model a Library Book

Easy ⏱ 8 min 88% acceptance ★★★★☆ 4.2
Define a class Book whose constructor takes title and author, storing both as instance attributes, plus a method describe() returning '<title> by <author>'. The warm-up every OOP interview starts with: attributes in __init__, behavior in methods.

Examples

Example 1
Input
Book('Dune', 'Herbert').describe()
Output
'Dune by Herbert'
Explanation

Attributes set in __init__ feed the method.

Constraints

  • Both attributes set in __init__.
  • describe returns the exact format.

Topics

OOPclass basics

Companies

TCSInfosysCognizant

Hints

Hint 1

self.title = title binds the attribute.

Hint 2

An f-string builds the description.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.