f-string 中的函数返回字符串外部的函数,并返回字符串内部的 None
function within f-string is returning the function outside the string, and returning None inside the string
我是 python 的新手,正在尝试按如下方式制作 f 弦:
next_patient = East_Room.get_highest_priority()
print(f"The next patient is {next_patient.display_symptoms()} please")
其中 East_Room 是 Class 的实例,get_highest_priority 是 class 中的一种方法,用于显示具有 [=25] 的最高整数的患者=]属性如下:
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}")
这是输出:
康纳:纳索
下一位患者是None
我知道这个方法是有效的,因为如果我在没有 f 字符串的情况下调用它,它会完美地工作。谢谢你的帮助!
display_symptoms
只打印信息但不 return 任何东西。
在 Python 中,没有 return 任何东西的函数 return None
,因此您得到的输出是:“下一位患者是None请
如果你还想让函数return这个字符串,你必须显式return它:
def display_symptoms(self):
print(f"{self.firstname} {self.lastname}: {self.symptoms}")
return f"{self.firstname} {self.lastname}: {self.symptoms}"
更好的方法是将其设为 属性:
@property
def display_symptoms(self):
return f"{self.firstname} {self.lastname}: {self.symptoms}"
我是 python 的新手,正在尝试按如下方式制作 f 弦:
next_patient = East_Room.get_highest_priority()
print(f"The next patient is {next_patient.display_symptoms()} please")
其中 East_Room 是 Class 的实例,get_highest_priority 是 class 中的一种方法,用于显示具有 [=25] 的最高整数的患者=]属性如下:
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}")
这是输出:
康纳:纳索
下一位患者是None
我知道这个方法是有效的,因为如果我在没有 f 字符串的情况下调用它,它会完美地工作。谢谢你的帮助!
display_symptoms
只打印信息但不 return 任何东西。
在 Python 中,没有 return 任何东西的函数 return None
,因此您得到的输出是:“下一位患者是None请
如果你还想让函数return这个字符串,你必须显式return它:
def display_symptoms(self):
print(f"{self.firstname} {self.lastname}: {self.symptoms}")
return f"{self.firstname} {self.lastname}: {self.symptoms}"
更好的方法是将其设为 属性:
@property
def display_symptoms(self):
return f"{self.firstname} {self.lastname}: {self.symptoms}"