0
class Employee():
    def __init__(self, name, salary=0):
        self.name = name
        self.salary = salary

    def hire(self):
        hired = input('is employee hired? enter yes or no')
        if 'yes' in hired:
            hiresalary = int(input('enter employee salary'))
            self.salary = hiresalary

    def fire(self):
        fired = input('is employee fired? enter yes or no')
        if 'yes' in fired:
            employeelist.remove(self.name)
        if 'no':
            pass
    def raise_salary(self):
        input("do you want to raise {}'s salary, enter yes or no".format(self.name))
        if 'yes':
           raise_sum =  int(input("by how much do you want to raise {} salary?".format(self.name)))
           self.salary += raise_sum
        if 'no':
            pass

I want to know if there's a way that every instance of the class will be stored in a list so that if i call the fire method i can delete that instance from the list.

  • 3
    Possible duplicate of [Printing all instances of a class](https://stackoverflow.com/questions/328851/printing-all-instances-of-a-class) – clubby789 Oct 08 '19 at 22:48
  • 1
    There's no simple builtin way, but if you simply append each instance to a list upon creation, you can do it manually. – clubby789 Oct 08 '19 at 22:48
  • 3
    I would say the design is wrong if the Employee class stores the list of Employees. Perhaps a Team or Company class might be better to store the list. – Paul Rooney Oct 08 '19 at 22:52

1 Answers1

0

As people above said, you would ideally store a list of employees outside of your Employee class. You could then change your Employee.fire() method to accept the list of employees like so:

employees = []

class Employee():
    def __init__(self, name, salary=0):
        self.name = name
        self.salary = salary

    def hire(self):
        hired = input('is employee hired? enter yes or no')
        if 'yes' in hired:
            hiresalary = int(input('enter employee salary'))
            self.salary = hiresalary

    def fire(self, employeelist):
        fired = input('is employee fired? enter yes or no')
        if 'yes' in fired:
            employeelist.remove(self)
        return employeelist

    def raise_salary(self):
        input("do you want to raise {}'s salary, enter yes or no".format(self.name))
        if 'yes':
           raise_sum =  int(input("by how much do you want to raise {} salary?".format(self.name)))
           self.salary += raise_sum
        if 'no':
            pass

bob = Employee('Bob', 50)
employees.append(bob)

sally = Employee('Sally', 75)
employees.append(sally)
employees = bob.fire(employees)
Mason Caiby
  • 1,846
  • 6
  • 17