I’m trying to put the body of the definition of a class inside a try: statement. The constructor (init()) takes a list (or many values - it doesn’t matter). I want to execute the body of the class defining instance variables and methods only if certain conditions on the data types of the elements in the lists are met. For instance, I want the entry 0 (I shall use python position-based indexing) to be a string and the rest to be either float or int. Now, obviously I have check these condition after the init takes the list as a variable. If these are not met, not only I want the constructor to stop doing anything else, but also the methods above to not be run; effectively, if the conditions are not met, I would like the class to be equivalent to class “My_Class: <return + indent> pass:” (sorry if the written code jargon is wrong).
This is what I have so far and seem to work. But it’s messy. I appreciate Ideas.
code:
# WITH EXCPETION HANDLING
class Patient:
def __init__(self, lst):
def checker(lst):
for i in range(1, len(lst)):
if type(lst[i]) in [int, float]:
continue
else:
return False
return True
if (lst.__class__() == []) and (type(lst[0]) == str) and checker(lst) == True:
self.name = lst[0]
self.age = lst[1]
self.sex = lst[2]
self.bmi = lst[3]
self.num_of_chidren = lst[4]
self.smoker = lst[5]
else:
print('invalid data') #this won't work, I know. break? I have no clue
try:
def checker(lst):
for i in range(1, len(lst)):
if type(lst[i]) in [int, float]:
continue
else:
return False
return True
if (lst.__class__() == []) and (type(lst[0]) == str) and checker(lst) == True:
def estimated_insurance_cost(self):
estimated_cost = 250*self.age -128*self.sex
print("{Patient_Name}’s estimated insurance costs is {estimated_cost} dollars.".format(Patient_Name = self.name, estimated_cost = estimated_cost))
def update_age(self, new_age):
self.age = new_age
print('{Patient_Name} is now {Patient_Age} years old.'.format(Patient_Name = self.name, Patient_Age = self.age))
self.estimated_insurance_cost()
def update_num_children(self,new_num_children):
self.num_of_children = new_num_children
if self.num_of_children == 1:
print("{} has {} child.".format(self.name, self.num_of_children))
else:
print("{} has {} children.".format(self.name, self.num_of_children))
self.estimated_insurance_cost()
def patient_profile(self):
patient_information = {'Name':self.name, 'Age': self.age, 'Sex': self.sex, 'BMI': self.bmi, 'Children': self.num_of_children, 'Smoker': self.smoker}
return patient_information
except:
def estimated_insurance_cost(self):
print('obj has invalid data')
def update_age(self, new_age):
print('obj has invalid data')
def update_num_children(self,new_num_children):
print('obj has invalid data')
def patient_profile(self):
print('obj has invalid data')
### you may try this as follows
patient_data = ["John Doe", 25, 1, 22.2, 0, 0]
patient1 = Patient(patient_data)
patient1.estimated_insurance_cost()
fake_data = ["John Doe", 'abc', 1, 22.2, 0, 0]
patient1 = Patient(fake_data)
patient1.estimated_insurance_cost()
I appreciate, thanks!