Zimano
Zimano

Reputation: 2299

Using certain attributes of an object to make other objects in a different class

For a program that creates a timetable for a doctor(specialist) I want to use certain attributes of an object created by a different class to be used in the class that makes the timetable for the doctor.

class makePatient(object):
  def __init__(self,name,room):
      self.name = name
      self.room = room
  def getPatient(self):
      print(self.name)
      print(self.room)

class makeSpecialist(object):
  def __init__(self,name,specialization,timetable):
      self.name = name
      self.specialization = specialization
      self.timetable = timetable
  def getSpecialist(self):
       print(self.name)
       print(self.specialization)
       print(self.timetable)

class makeAgenda(object):
   def addAgenda(self):
      self.timetable.append()
#I want to append the name of the patient I have defined here.
      print(self.timetable)

patient1 = makePatient("Michael","101")
specialist1 = makeSpecialist("Dr. John","Hematology",[])

What do I do now, to make sure that the name "Michael" gets added to the list [] of specialist Dr. John?

Thanks in advance, I will provide further details if necessary!

Upvotes: 0

Views: 103

Answers (3)

martineau
martineau

Reputation: 123443

Classes are often used to represent entities and operations allowed on them, include constructing, or making, new instances of them. Therefore, your classes would be better named simplyPatient, Specialist, andAgenda. The name of the method that constructs a new instance of any class in Python is always__init__().

That said, after creating aPatientand aSpecialistyou could then add patient instances to the specialist's timetable/agenda by passing it to aSpecialistmethod specifically designed for that purpose. In other words, a Specialist "has-a" Agenda instance namedtimetableand to which patients can be added via an appropriately namedadd_to_timetable()method.

Here's what I mean -- note I've modified your code to follow PEP 8 -- Style Guide for Python Code guidelines which I also suggest that you follow:

class Agenda(object):
    def __init__(self):
        self.events = []
    def append(self, event):
        self.events.append(event)

class Patient(object):
    def __init__(self, name, room):
        self.name = name
        self.room = room
    def get_patient(self):
        print(self.name)
        print(self.room)

class Specialist(object):
    def __init__(self, name, specialization):
        self.name = name
        self.specialization = specialization
        self.timetable = Agenda()
    def add_to_timetable(self, patient):
        self.timetable.append(patient)
    def get_specialist(self):
        print(self.name)
        print(self.specialization)
        print(self.timetable)

specialist1 = Specialist("Dr. John", "Hematology")
patient1 = Patient("Michael", "101")
specialist1.add_to_timetable(patient1)

Upvotes: 1

jonrsharpe
jonrsharpe

Reputation: 121987

I think another approach would be better; you can put the whole makePatient object into the timetable for the specialist:

specialist1 = makeSpecialist("Dr. John", "Hematology", [patient1])

Now you can access the names and other attributes of the patients in a specialist's timetable:

for patient in specialist1.timetable:
    print(patient.name)

You can also define a __repr__ method to tell Python how to display an object, rather than the current getPatient:

class makePatient(object):
    # ...
    def __repr__(self):
        return "{0} (room {1})".format(self.name, self.room)

Now when you print the whole timetable:

>>> print(specialist1.timetable)

You get the necessary information:

[Michael (room 101)] 

Note also that the classes should probably be called, simply, Patient, Specialist and Agenda; the make is implied.

Finally, you will get errors in makeAgenda.addAgenda as, without an __init__, self.timetable doesn't exist for a makeAgenda object, and an empty append() doesn't do anything anyway.

Upvotes: 2

Chris Barker
Chris Barker

Reputation: 2389

I'm not too sure what you're trying to accomplish here with method that just print values or with the makeAgenda class, but here's how you can get Michael in Dr. John's list:

specialist1.timetable.append(patient1.name)

Upvotes: 0

Related Questions