I. Objective Demonstrate composition in Python (a class that contains another).
ID: 3775004 • Letter: I
Question
I. Objective Demonstrate composition in Python (a class that contains another). II. Overview At this point, you should put all of your classes and other code in the same file. Don't worry about separating it out into separate modules. Ill. Instructions Write a Python 3 program according to the following: Person: Create a class for a Person that contains a name and a birth year. The author of a book will be a Person object. Create an initializer for your Person class to set the default name to be "anonymous" and the year to be "unknown". Create a method, display that displays the Person's name and birth year in the format "name (b. year)Explanation / Answer
#!/usr/bin/python
class Person:
def __init__(self):
self.name = "anonymous"
self.byear = "unknown"
def display(self):
print self.name, " (b.", self.byear, ")"
class Book:
def __init__(self):
self.title = "untitled"
self.author = Person()
self.publisher = "unpublished"
def display(self):
print " ", self.title, " Publisher: ", self.publisher, " Author:"
self.author.display()
"This would create first object of Person class"
book1 = Book()
book1.display();
print " Please enter the following:"
book1.author.name = raw_input("Name:")
book1.author.byear = raw_input("Year:")
book1.title = raw_input("Title:")
book1.publisher = raw_input("Publisher:")
book1.display();