You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Database类中实现list_interviewed_candidates方法筛选已面试候选人

问题:为Database类实现list_interviewed_candidates方法输出已面试候选人

我有用于记录求职者信息的Employee类,代码如下:

class Employee:
   def __init__(self, name, role, id):
      self.name = name
      self.role = role
      self.id = id
      self.interviewed = False

   def __str__(self):
      text = f'Candidate {self.name}; {self.id}. '
      if self.interviewed == False:
         return text + 'Not interviewed yet.'
      else:
         return text + 'Interviewed.'

   def interview(self):
      self.interviewed = True

同时还有用于存储特定雇主所有求职者的Database类:

class Database:
   def __init__(self, company, employer):
      self.company = company
      self.employer = employer
      self.candidates = []

   def __str__(self):
      text = f'The hiring company is {self.company} and the employers name is {self.employer}'
      return text

   def add_candidate(self, candidate):
      self.candidates.append(candidate)

我已经通过add_candidate方法将Employee实例添加到Database中,现在需要在Database类中新增list_interviewed_candidates(self)方法,输出所有interviewed属性为True的候选人。尝试的代码无法正常工作,用列表推导式也似乎无法访问Employee类的interviewed布尔属性。理想输出如下:

database1 = Database('Google', 'Jack H')
print(database1) 
# 输出:The hiring company is Google and the employers name is Jack H

candidate1 = Employee('Anna S', 'web-designer', 12)
database1.add_candidate(candidate1)
print(database1.list_interviewed_candidates()) 
# 输出:[]

candidate1.interview()
print(database1.list_interviewed_candidates()) 
# 输出:['Candidate Anna S; 12. Interviewed.']

问题分析

  1. 原list_interviewed_candidates方法使用return employee会直接返回第一个符合条件的候选人,无法返回所有已面试人员,不符合需求。
  2. 原Database类的__str__方法存在语法错误(f-string开头多了一个{),已修正。

修正后的完整代码

优化后的Database类(包含新增方法)

class Database:
   def __init__(self, company, employer):
      self.company = company
      self.employer = employer
      self.candidates = []

   def __str__(self):
      text = f'The hiring company is {self.company} and the employers name is {self.employer}'
      return text

   def add_candidate(self, candidate):
      self.candidates.append(candidate)

   def list_interviewed_candidates(self):
      # 用列表推导式筛选已面试候选人,并转为字符串格式
      return [str(employee) for employee in self.candidates if employee.interviewed]

测试验证

运行以下代码可得到理想输出:

# 创建数据库实例
database1 = Database('Google', 'Jack H')
print(database1) 
# 输出:The hiring company is Google and the employers name is Jack H

# 添加未面试候选人
candidate1 = Employee('Anna S', 'web-designer', 12)
database1.add_candidate(candidate1)
print(database1.list_interviewed_candidates()) 
# 输出:[]

# 标记候选人已面试
candidate1.interview()
print(database1.list_interviewed_candidates()) 
# 输出:['Candidate Anna S; 12. Interviewed.']

关键说明

  • 列表推导式可以直接访问Employee实例的interviewed属性,只要传入的是合法的Employee对象就不会有问题。
  • 使用str(employee)将实例转为__str__方法定义的字符串格式,完全匹配理想输出要求。
  • 原代码的return employee只会返回第一个匹配项,而列表推导式会收集所有符合条件的候选人,返回完整列表。

内容的提问来源于stack exchange,提问作者missingfours

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 07:15:41