0

I am a beginner in Python coding and I try to learn abstract classes. I have a problem with implementing two exams which extend abstract class Exam, defining contract for diagnostic test.

class Person:
def __init__(self, name: str, age: int):
self.name = name
self.age = age

class Patient(Person):
def __init__(self, name: str, age: int, id: str, symptoms: list\[str\], cholesterol: float, iron_level: float):
super().__init__(name, age)
self.id = id
self.symptoms = symptoms
self.cholesterol = cholesterol
self.iron_level = iron_level\`

class CholesterolExam(Exam):
def __init__(self):
super().__init__('Cholesterol Exam')

    def perform_exam(self,cholesterol_exam):
        if cholesterol_exam >= 200:
            return False
        return True

class IronLevelExam(Exam):
def __init__(self):
super().__init__('Iron Exam')

    def perform_exam(self,iron_level_exam):
        if iron_level_exam>50 and iron_level_exam<170:
            return True    

class Test03(unittest.TestCase):
    def test_doctor(self):
        cholesterol_exam = CholesterolExam()
        iron_level_exam = IronLevelExam()
        p = Patient('John', 30, '001', ['cough', 'fever'], 180, 300)
        cholesterol_exam_result, iron_exam_result = [exam.perform_exam(p) for exam in [cholesterol_exam, iron_level_exam]]

        self.assertTrue(cholesterol_exam_result)
        self.assertFalse(iron_exam_result)

test(Test03)

I get the error message:

line 32, in test_doctor cholesterol_exam_result, iron_exam_result = [exam.perform_exam(p) for exam in [cholesterol_exam, iron_level_exam]]

line 12, in perform_exam if cholesterol_exam >= 200:

TypeError: '>=' not supported between instances of 'Patient' and 'int'

1
  • Please edit your post and fix the indentation. It's very hard to understand this with the code (not) being indented the way it is. Also, can you share the base Exam class? Commented Dec 29, 2024 at 21:32

1 Answer 1

0

You are comparing a Patient object with 200, which is an Integer, but the actual thing you should have compared the number with was, probably, a field from p.

You have two options:

  1. Either call exam.perform_exam with another parameter that resembles an int, such as p.cholesterol
  2. Assuming that exam.perform_exam needs a Patient, compare an integer field from the parameter against that 200.

This is not about the abstract class, but about Python's lack of type inference.

An example for the first case mentioned above:

cholesterol_exam_result, iron_exam_result = [exam.perform_exam(p.cholesterol) for exam in [cholesterol_exam, iron_level_exam]]
Sign up to request clarification or add additional context in comments.

2 Comments

OK, thank you. And what should I change in the method code to keep this part: "[exam.perform_exam(p) for exam in [cholesterol_exam, iron_level_exam]]" unchanged? def perform_exam(self,cholesterol_exam): if cholesterol_exam >= 200: return False return True
@Mark it depends on what you want to check. At this moment, you compare the patient with 200. Do you want to compare the cholesterol level? If so, use if cholesterol_exam.cholesterol >= 200. Maybe also rename the parameter from cholesterol_exam to patient. It would make much more sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.