Forum Archive

Instance attribute as the value of another class instant attribute

resserone13

Is there a way I can have an attribute from one class received the value of an attribute from another class?

JonB

Yes......

resserone13

@JonB ok. I’ll keep researching. Thanks

JonB

Maybe a few more words description, or context might be helpful!

resserone13

@JonB I figured. I was just messing with you back hahaha. And I will to do the research. I usually learn 10 things that I wasn’t looking for before I find the one thing I was looking for and then I end up using those 10 things later...

What I was thinking if I had a patient class and a nurse class. I would like for every time a patient is created it to be added to a nurse. Or I would like a doctors to be added to a patient depending on the illness of the patient. So each patient will have an attribute called illness and I would like the doctor class to know that illness and assign the appropriate doctor. Or If I have The start and end times of when rooms were cleaned from my room class how can I access those from the housekeeper class to use the same times for the housekeeper class. I am sure I can use in Heritance in someway but I didn’t want random things inheriting from each other.


master_patient_list = []
current_patient_list = []
iso_patient_list = []

iso_type_dict = {'covid': 'droplets', 'cdiff': 'contact', 'tuberculosis': 'airborne', 'influenza': 'droplets', 'reverse isolation': 'patient saftey'}

def count_patients(list):
        return len(list)

class Patient (object):

        def __init__(self, name='', age=None, gender= None, room_number=None, assigned_doctor=None, assigned_nurse=None, illness=None, iso_status=None, patient_record_nunber='AD000',patient_medications=[]):
                self.name = name
                self.age = age 
                self.gender = gender
                self.room_number = room_number
                self.assigned_doctor = assigned_doctor
                self.assigned_nurse = assigned_nurse
                self.illness = illness
                self.iso_status = iso_type_dict
                self.patient_record_number = next(self.patient_id_gen())
                self.patient_medications = list(patient_medications)
                self.add_patient()

        def add_patient(self):
                master_patient_list.append(self)
                current_patient_list.append(self)


        def remove_patient():
                pass

        def move_patient(self, new_room):
                self.room_number = new_room         

        def add_med(self, med):
                if med not in self.patient_medications:
                        self.patient_medications.append(med)

        def remove_med(self, med):
                if med in self.patient_medications:
                        self.patient_medications.remove(med)    

#generates patine records numbers
        def patient_id_gen(self):
                ''' generates patient id one at a time as patient object is created '''

                prefix = 'AD'
                for i in range(1000,9999):
                        yield f"{prefix}{i}"

        def get_patient_id(self):
                return self.patient_record_number

        def __str__(self):
                return f'Room: {self.room_number} \nName: {self.name} \nPhysician: {self.assigned_doctor} \nNurse: {self.assigned_nurse} \nIllness: {self.illness} \nIso status: {self.iso_status} \nPRN: {self.patient_record_number} \nMedication: {self.patient_medications}'    

        def __repr__(self):
                pass

#patient info           
patient1 = Patient()
patient1.name = 'Sally'
patient1.age = 35
patient1.room_number = 430
patient1.assigned_doctor = 'Dr. Seuss'
patient1.assigned_nurse = 'Jessica'
patient1.illness = 'Broken Leg'
patient1.iso_status = 'NONE'
patient1.add_med('norco')

class Nurse ():

        def __init__(self, patient):
                self.patient = Patient.self.name
                patient_list = [self.patient]
                patient_count = len(patient_list)

        def __str__():
                return f'{name} \n{room_number} \n{assigned_doctor}'        

class Physician (Employee):
        pass


bennr01

First of, you can move your *_patient_list into the Patient class to reduce global variables.

class Patient(object):
    master_patient_list = []  # this should be shared among all Patients
    def __init__(self, ...):
        ...
        self.master_patient_class.append(self)

Second, I think you want something like this for example:

class Physician(Employee):
    illness2doctor = {}  # shared dict mapping illness -> doctor for illness

    def __init__(self, name):
         # just as example
         self.name = name

    @classmethod
    def register_doctor(cls, doctor, illness):
        assert not illness in cls.illness2doctor  # prevent overwrite
        cls.illness2doctor[illness] = doctor

    @classmethod
    def get_doctor_for(cls, patient):
        illness = patient.illness
        if illness not in cls.illness2doctor:
            raise KeyError("No doctor for illness")
        return cls.illness2doctor[illness]


# --- usage ---
# create a new physician
alice = Physician(name="Alice")
# alice can treat "Broken Leg"
Physician.register_doctor(alice, "Broken Leg")

# now we can get the doctor for patient1
print(Physician.get_doctor_for(patient1).name)
# -> should print "Alice"