Home > Back-end >  Calling a class method
Calling a class method

Time:06-10

How to call a class method on object which doesn't have a name? I don't know how to ask this question, so:

First, I have a class Patient with _ init _ that creates patients: Patient(id, name, ..., appointments). Instances of the class.

Then I have a staticmethod add_patient with inputs, which ending somehow like this:

new = Patient(id, name, ..., appointments)
patientsdictionary[new.id] = new

It works and everything is okay, imagine that I have added a few patients. Then I want to make an appointment with function get_appointment(self) which is supposed to add something to the appointments list in specific instance - like e.g. I want to add sth to list appointments in Patient object with has id=3.

Usually in code it would be called as name.get_appointment() I guess, but my patients don't really have names like 'patient1=Patient(...)', they are just in a dictionary. How to do it?

CodePudding user response:

def add_appointment_to_patient(patient_id, *args):
    patient_obj = patientsdictionary[patient_id]
    # add appointment to patient object
    patient_obj.get_appointments(*args)

You can access the patient object if you know the patient id and can access the patient dict. From there you can modify the object through your aforementioned get_appointments method.

  • Related