Home > other >  Create a method that lists instances that were set to True in a previous class
Create a method that lists instances that were set to True in a previous class

Time:10-25

I have the following code that records job candidates' personal details in a class 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

I also have another class Database that lists all the candidates in a database for a particular employer:

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)

Now, if we record personal details of two candidates in the class Employee and add them to the class Database using the method add_candidate, how do I create a new method called list_interviewed_candidates(self) in the class Database that will print all candidates that have self.interviewed set to True?

This is what I tried:

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

But that doesn't work. I have also tried list comprehension but it seems I just cannot access the boolean value that was set in the first class. Ideally, the output should look something like this:

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']

CodePudding user response:

In your Database.list_interviewed_candidates method you are returning the first employee that was interviewed. Keep in mind that return exits from the current function (method in this case) as soon as is hit.

So it starts looking at your candidates and as soon as one interviewed is found, it returns that one.

You probably want to gather them all in a list and return that:

   def list_interviewed_candidates(self):
      retval = []
      for employee in self.candidates:
         if employee.interviewed == True:
            retval.append(employee)
      return retval

Something pretty interesting you could also use is yield:

   def list_interviewed_candidates(self):
      for employee in self.candidates:
         if employee.interviewed == True:
            yield employee

Which... you can try by doing:

print(list(database1.list_interviewed_candidates()))

Pretty cool. This opens the really fun world of iterators!

CodePudding user response:

A list comprehension works, but realize __str__ is used by print but __repr__ is used for items displayed in a list. __repr__ is also used if __str__ isn't defined.

Try the following, but change __str__ to __repr__ in Employee:

def list_interviewed_candidates(self):
    return [employee for employee in self.candidates if employee.interviewed]

Then:

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()) 

Outputs:

The hiring company is Google and the employers name is Jack H
[]
[Candidate Anna S; 12. Interviewed., Candidate Mark T; 13. Interviewed.]

Customize __str__ and __repr__ individually if you want differing output between direct print of Employee and how it is displayed in a list.

  • Related