我有以下代碼在班級中記錄求職者的個人詳細資訊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)
現在,如果我們在 中記錄兩個候選人的個人詳細資訊class Employee并將它們添加到class Databaseusing 方法add_candidate中,我如何創建一個在 中呼叫的新方法list_interviewed_candidates(self),該方法class Database將列印所有已self.interviewed設定為 True 的候選人?
這是我嘗試過的:
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):
for employee in self.candidates:
if employee.interviewed == True:
return employee
但這不起作用。我也嘗試過串列理解,但似乎我無法訪問在第一類中設定的布林值。理想情況下,輸出應如下所示:
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 Ana S; 12 - Interviewed']
uj5u.com熱心網友回復:
在您的Database.list_interviewed_candidates方法中,您將回傳第一個接受采訪的員工。請記住,return一旦被擊中,就會退出當前函式(在這種情況下為方法)。
因此,它會開始查看您的候選人,一旦找到接受采訪的候選人,它就會回傳該候選人。
您可能希望將它們全部收集在一個串列中并回傳:
def list_interviewed_candidates(self):
retval = []
for employee in self.candidates:
if employee.interviewed == True:
retval.append(employee)
return retval
您還可以使用的一些非常有趣的東西是yield:
def list_interviewed_candidates(self):
for employee in self.candidates:
if employee.interviewed == True:
yield employee
哪個...您可以嘗試這樣做:
print(list(database1.list_interviewed_candidates()))
很酷。這打開了真正有趣的迭代器世界!
uj5u.com熱心網友回復:
串列推導有效,但實作__str__由print但__repr__用于串列中顯示的專案。 __repr__如果__str__未定義,也使用。
嘗試以下操作,但更改__str__為__repr__in Employee:
def list_interviewed_candidates(self):
return [employee for employee in self.candidates if employee.interviewed]
然后:
database1 = Database('Google', 'Jack H')
print(database1)
candidate1 = Employee('Anna S', 'web-designer', 12)
candidate2 = Employee('Mark T', 'pythonista', 13)
database1.add_candidate(candidate1)
database1.add_candidate(candidate2)
print(database1.list_interviewed_candidates())
candidate1.interview()
candidate2.interview()
print(database1.list_interviewed_candidates())
輸出:
The hiring company is Google and the employers name is Jack H
[]
[Candidate Anna S; 12. Interviewed., Candidate Mark T; 13. Interviewed.]
如果您希望在直接輸出和串列中顯示方式之間有不同的輸出,則可以單獨__str__自定義。__repr__printEmployee
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/521504.html
標籤:Python班级方法
