In: Computer Science
1) What is the argument in “class AddressBook(object):”
2) What is the roll of “def __init__(self):”?
3) What is the roll of “def __repr__ (self):”?
4) Please copy and run “Addressbook” Python program.
Submit the code and the output
5) Discuss the two outputs’ differences (data type).
6) Please add 2 more people and report the output.
Code:
class AddressBook(object):
def init_(self):
self.people=[]
def add_entry(self, new_entry):
self.people.append(new_entry)
class AddressEntry(object):
def __init__(self, first_name=None, family_name= None,
email_address= None, DOB= None):
self.first_name = first_name
self.family_name = family_name
self.email_address = email_address
self.DOB = DOB
def __repr__(self):
template = "AddressEntry(first_name='%s', "+\
"family_name= '%s', "+\
"email_address= '%s', "+\
"DOB= '%s')"
return template%(self.first_name, self.family_name,
self.email_address, self.DOB)
if __name__== "__main__":
address_book = AddressBook()
person1= AddressEntry("Tom", "Smith", None, "Oct 24, 1999")
print(person1)
address_book.new_entry(person1)
print(address_book.people)
1.) Argument in class AddressBook is an object of class AddressEntry.
2.) Role of “def __init__(self):" is to to initialize all the member variable of class as and when any instance of a class is created. The code written inside __init__ will initialize the member variable of the object with the value mentioned in the code.
3) Role of “def __repr__ (self):” is to return the printable representation of the given object. It can also be called as the representation of object in string form. It takes one parameter which is the object. When we need to give the hint to the developer or the user as to what an object means along with some useful information, we use __repr__ function. It is mainly used in debugging in the development or maintenance phase of the code.
4.)
class AddressBook(object):
people = []
def init_(self):
self.people=[]
def add_entry(self, new_entry):
self.people.append(new_entry)
class AddressEntry(object):
def __init__(self, first_name=None, family_name= None, email_address= None, DOB= None):
self.first_name = first_name
self.family_name = family_name
self.email_address = email_address
self.DOB = DOB
def __repr__(self):
template = "AddressEntry(first_name='%s', "+\
"family_name= '%s', "+\
"email_address= '%s', "+\
"DOB= '%s')"
return template%(self.first_name, self.family_name, self.email_address, self.DOB)
if __name__== "__main__":
address_book = AddressBook()
person1= AddressEntry("Tom", "Smith", None, "Oct 24, 1999")
print(person1)
address_book.add_entry(person1)
print(address_book.people)