Home > Software engineering >  function within f-string is returning the function outside the string, and returning None inside the
function within f-string is returning the function outside the string, and returning None inside the

Time:12-16

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

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

This is the output:

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!

CodePudding user response:

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}"
  • Related