[Python] Implementing abstract classes

Discussão em 'Python' iniciado por Stack, Abril 24, 2025.

  1. Stack

    Stack Membro Participativo

    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'
    ​

    Continue reading...

Compartilhe esta Página