0

I've created a class named Patient that has attributes for patient information. I'm supposed to use an accessor and mutator method for each attribute. Then I've created another file to access the class and insert patient information to print. Every time I print I don't get what I expect but I get <Assignment4Q1PatientClass2nd.Patient object at 0x000002429E038A00>.

Here's what is on my first file (File name is Assignment4Q1PatientClass2nd):

class Patient:
    
    def __init__(self, fname, mname, lname, address, city, state, zipcode, phone, ename, ephone):
        self._fname = fname       #first name
        self._mname = mname       #middle name
        self._lname = lname       #last name
        self._address = address   #address
        self._city = city         #city for address
        self._state = state       #state for address
        self._zipcode = zipcode   #zipcode for address
        self._phone = phone       #phone number
        self._ename = ename       #emergency name
        self._ephone = ephone     #emergency phone
        
    #add patient information    
    def addFirstName(self, firstname):
        self._fname = self._fname + firstname 
    def addMiddleName(self, middlename):
        self._mname = self._mname + middlename 
    def addLastName(self, lastname):
        self._lname = self._lname + lastname
    def addAddress(self, locaddress):
        self._address = self._address + locaddress
    def addCity(self, cityname):
        self._city = self._city + cityname 
    def addState(self, statename):
        self._state = self._state + statename
    def addZipcode(self, zipcodenum):
        self._zipcode = self._zipcode + zipcodenum
    def addPhone(self, phonenum):
        self._phone = self._phone + phonenum
    def addEName(self, emergencyname):
        self._ename = self._ename + emergencyname
    def addEPhone(self, emergencyphone):
        self._ephone = self._ephone + emergencyphone
        
    #get/return all information of the Patient
    def getPatientFirstName(self):
        return "First Name:" + self._fname
    def getPatientMiddleName(self):
        return "Middle Name:" + self._mname
    def getPatientLastName(self):
        return "Last Name:" + self._lname 
    def getPatientAddress(self):
        return "Address:" + self._address  
    def getPatientCity(self):
        return "City:" + self._city 
    def getPatientState(self):
        return "State:" + self._state
    def getPatientZipcode(self):
        return "ZIP:" + self._zipcode   
    def getPatientPhone(self):
        return "Phone:" + self._phone 
    def getPatientEName(self, emergencyname):
        return "Emergency Contact:" + self._ename 
    def getPatientEPhone(self, emergencyphone):
        return "Emergency Phone:" + self._ephone

on the second file is:

from Assignment4Q1PatientClass2nd import Patient

pat = Patient("James", "Edward", "Jones", "345 Main Street", "Billings", "Montanna", 59000, "406-555-1212", "Jenny Jones", "406-555-1213")
    
print(pat)
Matthias
  • 12,873
  • 6
  • 42
  • 48
J G
  • 1
  • Does this answer your question? [How to print instances of a class using print()?](https://stackoverflow.com/questions/1535327/how-to-print-instances-of-a-class-using-print) – SuperStormer Feb 15 '23 at 21:41

1 Answers1

0

What did you expect from your print statement? The class actually don't "know" what to print. You must provide a way to represent that class as a string, so we can print that string.

In practice, we do this by adding a function called "__repr__", the representation of this class. Python automatically identifies this as a especial one, just like "__init__".

Here is a small example to you:

class Patient:
    def __init__(self, name):
        self._name = name
    def getPatientName(self):
        return self._name
    def __repr__(self):
        return "Hey! My name is " + self.getPatientName()

pat = Patient("Dikson")
print(pat)
# Hey! My name is Dikson

Hope it's clear :)

diksown
  • 182
  • 1
  • 9
  • By the way, your mutator is **adding** new information to the current information, instead of overwriting it. Consider using `self._fname = firstname` instead of `self._fname = self._fname + firstname` and changing the name of the function. What about `changeFirstName`? And welcome to coding! – diksown Nov 08 '22 at 05:04