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

function within f-string is returning the function outside the string, and returning None inside the string

I’m new to python and trying to do an f-string as follows:

next_patient = East_Room.get_highest_priority()
print(f"The next patient is {next_patient.display_symptoms()} please")

Where East_Room is an instance of a Class and get_highest_priority is a method within the class to display a patient with the highest integer for the ‘severity’ attribute as follows:

def get_highest_priority(self):
    tmp_priority_patient = None
    current_size = self.SLL_waiting_list.size()     
    counter = 1
    while counter <= current_size:
        tmp_node = self.SLL_waiting_list.get_node(counter)
        tmp_patient = tmp_node.get_obj()
        if tmp_priority_patient == None:
            tmp_priority_patient = tmp_patient
        else:
            if tmp_patient.severity > tmp_priority_patient.severity:
                tmp_priority_patient = tmp_patient
        counter = counter + 1
    return tmp_priority_patient

This is the output:

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

Conor : Naseau

The next patient is None please

I know that this method works as it works perfectly if I call it without the f-string. thanks for you help!

>Solution :

display_symptoms only prints information but doesn’t return anything.

In Python, function that don’t return anything return None, hence the output you got: "The next patient is None please"

If you also want the function to return this string, you have to explicitly return it:

def display_symptoms(self):
    print(f"{self.firstname} {self.lastname}: {self.symptoms}")
    return f"{self.firstname} {self.lastname}: {self.symptoms}"

An even better way to do it would be to make it a property:

@property
def display_symptoms(self):
    return f"{self.firstname} {self.lastname}: {self.symptoms}"
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