# Chapter 5 page 54 # define class using the class keyword # class is a blueprint for objects # Contact Class 3 objects fname, lname, age(int) # use the __init__ built in method to define the attributes of the class ######### Contact Class Blueprint ################### class Contact: # defines the NAME of our class # attribute (aka. global class variable) to be used by Class Methods page 57 total_contacts = 0 # Instance method page 55 def __init__(self, fname, lname, age): # property or var name = param self.fname = fname # public string attr self.lname = lname self.age = age # public int attr Contact.total_contacts += 1 # increment # Contact.total_contacts + 1 # addition operator # _hidden variable/attr/property page 59 self._vipStatus = True # boolean default assignment # getter to get the value assigned # @property decorator to type change from attr to property # property(attr) @property def vipStatus(self): return self._vipStatus # setter page 59 @vipStatus.setter def vipStatus(self, x): # x is a param that you will assign calling of the method # conditional testing # if the value is already set to True self._vipStatus = x # what is passed when we call the method ### Example of call in program ### ### new_contact1._vipStatus = False ### # Class method page 57 @classmethod # decorator has to be one the line above the def keyword def display_total_contacts(cls): return cls.total_contacts # Instance method page 55 def contact_info(self): #print(f"{self.fname} {self.lname} {self.age}") # format {template variable} return f"{self.fname} {self.lname} {self.age}" # Static method page 57 @staticmethod def is_adult(age): return age >= 18 # True False ##################################################### ############## Contact Details Class ################ # Inheritance page 60-62 class ContactDetails(Contact): def __init__(self, fname, lname, age, email, department): # 5 # Contact.__init__(self,fname,lname,age) method 1 # method 2 super() super().__init__(fname, lname, age=None) # 3 attr parent # 2 attrs child self.department = department self.email = email def contact_fname_dept(self): return f'First Name: {contact_details.fname} Department: {contact_details.department}.' ##################################################### ####### Program main() "Run" ###################### def contact_information(fname, lname, age): print(f"{fname} {lname} {age}") # Create an instance of the Contact class and pass the values as arg # new_contact is the instance name = ContactClass(params) new_contact1 = Contact("Philip", "Matusiak", 21) new_contact2 = Contact("Bob", "Smith", 32) # new_contact.contact_info() >>> IF print() is already returned from method() or call print() as a part of the main() program print(new_contact1.contact_info()) print(new_contact2.contact_info()) # total number of contacts added print(Contact.display_total_contacts()) # Asserting 2 in Output # call the static method and return the boolean page 57 print(Contact.is_adult(52)) # True print(Contact.is_adult(14)) # False #age = int( input("Enter contact age: ")) age = 32 print(Contact.is_adult(age)) # get the current value of vipStatus #True print("==== Return of Getter =====") print(new_contact1._vipStatus) # calling a setter of the class reset the vipStatus #set vipStatus False new_contact1._vipStatus = False # get the new value of vipStatus that was just set #False print("==== Return of Getter after Setting to new value =====") print(new_contact1._vipStatus) # Inheritance create instance of ContactDetails child class page 62 contact_details = ContactDetails("Mary", "Jones", 32, "mary@gmail.com", "Human Resources") print("Using the instance variables in the program.") print(f'First Name: {contact_details.fname} Department: {contact_details.department}.') print("Using the instance method in the class.") print(contact_details.contact_fname_dept())