Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Calling a class method

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading