a class definition named Book. The Book class should have data attributes for a book’s title, the author’s name, and the publisher’s name. The class should also have the following:
a. An __init__ method for the class. The method should accept an argument for each of the data attributes.
b. Accessor and mutator methods for each data attribute.
c. An __str__ method that returns a string indicating the state of the object.
class Book:
def __init__(self, title, author, publisher):
"""
Initializes a Book object with the given title, author, and publisher.
Args:
title (str): The title of the book.
author (str): The author's name.
publisher (str): The publisher's name.
"""
self.title = title
self.author = author
self.publisher = publisher
def get_title(self):
"""
Returns the title of the book.
Returns:
str: The title of the book.
"""
return self.title
def set_title(self, title):
"""
Sets the title of the book.
Args:
title (str): The new title of the book.
"""
self.title = title
def get_author(self):
"""
Returns the author's name.
Returns:
str: The author's name.
"""
return self.author
def set_author(self, author):
"""
Sets the author's name.
Args:
author (str): The new author's name.
"""
self.author = author
def get_publisher(self):
"""
Returns the publisher's name.
Returns:
str: The publisher's name.
"""
return self.publisher
def set_publisher(self, publisher):
"""
Sets the publisher's name.
Args:
publisher (str): The new publisher's name.
"""
self.publisher = publisher
def __str__(self):
"""
Returns a string indicating the state of the Book object.
Returns:
str: The string representation of the Book object.
"""
return f"Title: {self.title}\nAuthor: {self.author}\nPublisher: {self.publisher}"
# Create a Book object and test the methods
book1 = Book("Starting Out With Python", "Tony Gaddis", "ABC Publishers")
print(book1) # Prints the string representation of the Book object
book1.set_title("Python Learn") # Update the title
print(book1.get_title()) # Prints the updated title
book1.set_author("Charless Doe") # Update the author
print(book1.get_author()) # Prints the updated author
book1.set_publisher("XYZ Publications") # Update the publisher
print(book1.get_publisher()) # Prints the updated publisher
Executed Output:
Title: Starting Out With Python
Author: Tony Gaddis
Publisher: ABC Publishers
Python Learn
Charless Doe
XYZ Publications